2014-09-26 42 views
-1

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

清单文件:

<?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和现在工作的罚款。

+1

尝试将** target sdk版本更改为19 **请参阅http://stackoverflow.com/a/24652693/991085 – 2014-09-26 11:47:31

+0

对android:targetSdkVersion =“21”的使用android:targetSdkVersion =“19” ' – 2014-09-26 11:49:17

+0

'引起:android.util.AndroidRuntimeException:你不能梳理ine滑动解雇和行动栏' – 2014-09-26 11:51:09

回答

3

您可以参考以下链接以获取更好的主意,这个问题 android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar

来处理它,您还可以通过更改目标SDK版本19尝试延长“活动”。

+0

它与API的工作很好19.什么类型的错误是与我想知道的新版本>? – wayzz 2014-09-26 12:28:58

+0

因为你可以参考这个链接http://stackoverflow.com/questions/24501374/targetsdkversion-setting如上19在avd中不支持。 – Beena 2014-09-26 12:32:26

2

到位ActionbarActivity