Android新手。
我尝试在我的onClickListener中创建一个简单的意图,它将打开一个新页面。但是,当我单击按钮时,应用程序崩溃了。我不确定是什么问题。
这是我的倾听者
buttonCheat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cheatView = new Intent(getApplicationContext(),CheatActivity.class);
startActivity(cheatView);
}
这里我在我的清单文件中声明了活动
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".QuizActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CheatActivity"
android:label="@string/app_name" />
</application>
以下是相关日志
10-03 01:51:23.770 2630-2630/? D/CheatActivity﹕ onCreate() called
10-03 01:51:23.777 2630-2630/? D/AndroidRuntime﹕ Shutting down VM
10-03 01:51:23.777 2630-2630/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: itp341.exercises.week6.geoquiz, PID: 2630
java.lang.RuntimeException: Unable to start activity ComponentInfo{itp341.exercises.week6.geoquiz/itp341.exercises.week6.geoquiz.CheatActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.ComponentName.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.ComponentName.toString()' on a null object reference
at itp341.exercises.week6.geoquiz.CheatActivity.onCreate(CheatActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
来自CheatActivity
public class CheatActivity extends Activity {
private static final String TAG = "CheatActivity";
// View references
TextView textAnswer;
Button buttonShowAnswer;
// instance variables
boolean answerIsTrue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate() called");
setContentView(R.layout.activity_cheat);
Log.d(TAG, getCallingActivity().toString());
=
//find views
textAnswer = (TextView) findViewById(R.id.text_answer);
buttonShowAnswer = (Button) findViewById(R.id.button_show_answer);
}
}
答案 0 :(得分:1)
Intent cheatView = new Intent(getApplicationContext(),CheatActivity.class);
这就是存在异常的原因,尝试使用它来替换它,
Intent cheatView = new Intent(QuizActivity.this,CheatActivity.class);
因为应用程序上下文无法启动活动。