我是 android 编程新手,当我在调试时遇到错误时,我正在从教程中创建一个简单的应用程序。在本教程中,该应用程序包含一个按钮,用于计算它被按下的次数,并且该项目有效,但我编辑了该项目以创建一个具有两个按钮的应用程序,一个按钮计算它被按下的次数(使用简单的 if-else)和其他重置计数。这个修改后的应用程序不起作用。
这些是代码错误和代码文件:
错误:
11-25 21:47:32.888: E/AndroidRuntime(6865): FATAL EXCEPTION: main
11-25 21:47:32.888: E/AndroidRuntime(6865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flavio.tictactoe/com.flavio.tictactoe.TicTacToe}: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.os.Looper.loop(Looper.java:137)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-25 21:47:32.888: E/AndroidRuntime(6865): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 21:47:32.888: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-25 21:47:32.888: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-25 21:47:32.888: E/AndroidRuntime(6865): at dalvik.system.NativeStart.main(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865): Caused by: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865): at com.flavio.tictactoe.TicTacToe.onCreate(TicTacToe.java:24)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.Activity.performCreate(Activity.java:4465)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-25 21:47:32.888: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-25 21:47:32.888: E/AndroidRuntime(6865): ... 11 more
TicTacToe.java(是的,我知道它不是井字游戏应用程序 :)):
package com.flavio.tictactoe;
import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.view.View;
public class TicTacToe extends Activity
implements View.OnClickListener{
Button button;
int touchCount;
String number;
Button ResetButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = new Button(this);
button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);
ResetButton=new Button(this);
ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);
setContentView(R.id.button_count);
setContentView(R.id.button_reset);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button_count:
touchCount++;
if(touchCount>1){
number=" times";
}else{
number=" time";
}
button.setText("Touched me " + touchCount + number);
case R.id.button_reset:
touchCount=0;
if(touchCount>1){
number=" times";
}else{
number=" time";
}
}
}
}
activity_tic_tac_toe.xml:
<RelativeLayout 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"
tools:context=".TicTacToe" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button_count"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:id="@+id/button_reset"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</RelativeLayout>
PS 我的 android.jar 文件有问题:每次我想查看 .class 文件时,eclipse 都会告诉我“找不到源”