我写了这个简单的代码,但我不知道为什么我的模拟器总是向我显示这个错误:“应用程序已意外停止 android 请再试一次”。我试图摆脱它,但没有成功帮助我,谢谢。
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
sub = (Button) findViewById(R.id.bSub);
display = (Button) findViewById(R.id.tvDisplay);
add = (Button) findViewById(R.id.bAdd);
// *****onClick method****//
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter + 1;
display.setText("Your Total is " + counter);
}
});
}
}
//my xml//
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/total"
android:textSize="45sp" />
<Button
android:id="@+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/sub" />
</LinearLayout>
我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thenewbostonnn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.thenewbostonnn.MainActivity"
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>