我是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(是的,我知道这不是一个tic tac toe app :)):
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>
P.S。我的android.jar文件有问题:每次我想看一个.class文件时,eclipse告诉我'找不到来源'
答案 0 :(得分:2)
您需要先使用setContentView()
来显示activity_tic_tac_toe
布局。然后,您可以使用findViewById()
在此布局中获取按钮或任何其他视图:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe); // The layout that holds your Buttons
button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);
ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);
(请注意,我删除了一些不必要的行。)
P.S。我的android.jar文件有问题:每次我想看一个.class文件时,eclipse告诉我'找不到来源'
您是否正在尝试查看源代码?单击附加源代码并将其指向您的Android SDK副本。
如果您没有该选项,请阅读以下问题:How to attach javadoc or sources to jars in libs folder?