请帮助我几乎完成了我的代码,但是当我运行它时,我得到nullpointerexception .. 我试图在按钮点击时增加一个静态int变量, 他们是五项活动。在每个活动中,他们是一个问题,当正确回答时,当达到最后一页时,静态int必须递增1.我显示总数..
package com.example.kei;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
RadioButton correctChoice1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
correctChoice1.findViewById(R.id.correct1);
correctChoice1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AppSingleton.score++;
Intent intent = new Intent(MainActivity.this, Page_two.class);
startActivity(intent);
}
});
}
public static class AppSingleton {
public static int score= 0;
}
public void choice_a(View view){
Intent intent = new Intent(this, Page_two.class);
startActivity(intent);
}
public void choice_c(View view){
Intent intent = new Intent(this, Page_two.class);
startActivity(intent);
}
}
代码 choice_a 和 choice_c 是错误的答案。我尝试使用onlicklistener为radiobutton增加正确的答案。为什么代码不能执行并给出空指针异常?请帮忙..我不知道该怎么做..
logcat的:
03-25 05:21:04.811: D/dalvikvm(1052): Late-enabling CheckJNI
03-25 05:21:04.827: E/Trace(1052): error opening trace file: No such file or directory (2)
03-25 05:21:04.863: D/AndroidRuntime(1052): Shutting down VM
03-25 05:21:04.863: W/dalvikvm(1052): threadid=1: thread exiting with uncaught exception (group=0xa6246288)
03-25 05:21:04.867: E/AndroidRuntime(1052): FATAL EXCEPTION: main
03-25 05:21:04.867: E/AndroidRuntime(1052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kei/com.example.kei.MainActivity}: java.lang.NullPointerException
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.os.Looper.loop(Looper.java:137)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 05:21:04.867: E/AndroidRuntime(1052): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 05:21:04.867: E/AndroidRuntime(1052): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 05:21:04.867: E/AndroidRuntime(1052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 05:21:04.867: E/AndroidRuntime(1052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 05:21:04.867: E/AndroidRuntime(1052): at dalvik.system.NativeStart.main(Native Method)
03-25 05:21:04.867: E/AndroidRuntime(1052): Caused by: java.lang.NullPointerException
03-25 05:21:04.867: E/AndroidRuntime(1052): at com.example.kei.MainActivity.onCreate(MainActivity.java:18)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.Activity.performCreate(Activity.java:5008)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 05:21:04.867: E/AndroidRuntime(1052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-25 05:21:04.867: E/AndroidRuntime(1052): ... 11 more
03-25 05:33:23.686: E/Trace(1694): error opening trace file: No such file or directory (2)
03-25 05:33:23.718: D/AndroidRuntime(1694): Shutting down VM
03-25 05:33:23.718: W/dalvikvm(1694): threadid=1: thread exiting with uncaught exception (group=0xa6246288)
03-25 05:33:23.722: E/AndroidRuntime(1694): FATAL EXCEPTION: main
03-25 05:33:23.722: E/AndroidRuntime(1694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kei/com.example.kei.MainActivity}: java.lang.NullPointerException
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.os.Looper.loop(Looper.java:137)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 05:33:23.722: E/AndroidRuntime(1694): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 05:33:23.722: E/AndroidRuntime(1694): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 05:33:23.722: E/AndroidRuntime(1694): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 05:33:23.722: E/AndroidRuntime(1694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 05:33:23.722: E/AndroidRuntime(1694): at dalvik.system.NativeStart.main(Native Method)
03-25 05:33:23.722: E/AndroidRuntime(1694): Caused by: java.lang.NullPointerException
03-25 05:33:23.722: E/AndroidRuntime(1694): at com.example.kei.MainActivity.onCreate(MainActivity.java:23)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.Activity.performCreate(Activity.java:5008)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 05:33:23.722: E/AndroidRuntime(1694): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-25 05:33:23.722: E/AndroidRuntime(1694): ... 11 more
答案 0 :(得分:1)
将correctChoice1.findViewById(R.id.correct1);
替换为
correctChoice1 = (RadioButton )findViewById(R.id.correct1);
希望这有帮助。
答案 1 :(得分:0)
尝试初始化RadioButton
,如下所示:
correctChoice1= (RadioButton )findViewById(R.id.correct1);
correctChoice1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(AppSingleton.score <=5)
AppSingleton.score++;
Intent intent = new Intent(MainActivity.this, Page_two.class);
startActivity(intent);
}
});
答案 2 :(得分:0)
您的单选按钮声明错误:
这样做;
RadioButton correctChoice1;
correctChoice1 = (RadioButton).findViewById(R.id.correct1);