【问题标题】:Simple Android App not working [duplicate]简单的Android应用程序不起作用[重复]
【发布时间】:2014-09-26 11:46:16
【问题描述】:

您好,这是我的第一个安卓应用程序。我正在使用 Eclipse 并且正在关注 New Boston 教程。当我运行一个简单的应用程序来增加变量并显示结果时,应用程序显示“不幸的是应用程序已停止”。我访问过其他讨论并导入了必要的库,但它仍然给我这个结果。

清单文件:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapplicationdillon"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

活动 xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.firstapplicationdillon.Main" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your total is 0"
        android:textSize="45sp"
        android:layout_gravity="center"
        android:id="@+id/tvDisplay" />

    <Button
        android:layout_width="250sp"
        android:layout_height="wrap_content"
        android:text="Add One"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:id="@+id/bAdd"
        />
    <Button
        android:layout_width="250sp"
        android:layout_height="wrap_content"
        android:text="Subtract One"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:id="@+id/bSub"
        />

</LinearLayout>

活动主文件:

package com.example.firstapplicationdillon;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Main extends ActionBarActivity {

    int counter;
    Button add, sub;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your total is " + counter);
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Your total is " + counter);

            }
        });
    }
}

更新:我安装了 Android API 19,它现在可以正常工作了。

【问题讨论】:

  • 尝试将 target sdk 版本更改为 19 看到这个stackoverflow.com/a/24652693/991085
  • 插入android:targetSdkVersion="21" 使用android:targetSdkVersion="19"
  • Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar

标签: java android eclipse


【解决方案1】:

您可以参考以下链接以更好地了解此问题 android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar

为了处理它,你也可以通过将目标sdk版本更改为19来尝试。

【讨论】:

【解决方案2】:

扩展“Activity”代替ActionbarActivity

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多