我为我的应用程序的设置制作了布局xml。我有一个偏好,我想点击它将被发送到另一个活动。在settings.java的文件中,我输入以下代码来调用其他活动但是我收到错误。这是代码:
Preference info;
info.setOnPreferenceClickListener(new OnPreferenceClickListener()) {
public boolean onPreferenceClick(Preference preference) {
intent = new Intent(getBaseContext(), OtherActivity.class);
startActivity(intent);
}
}
我该如何解决?感谢
这是错误:
07-03 14:43:46.314:E / AndroidRuntime(9691):致命异常:主要 07-03 14:43:46.314:E / AndroidRuntime(9691):java.lang.RuntimeException:无法启动活动ComponentInfo {com.XXX}:java.lang.NullPointerException 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.os.Handler.dispatchMessage(Handler.java:99) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.os.Looper.loop(Looper.java:137) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread.main(ActivityThread.java:5039) 07-03 14:43:46.314:E / AndroidRuntime(9691):at java.lang.reflect.Method.invokeNative(Native Method) 07-03 14:43:46.314:E / AndroidRuntime(9691):at java.lang.reflect.Method.invoke(Method.java:511) 07-03 14:43:46.314:E / AndroidRuntime(9691):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 07-03 14:43:46.314:E / AndroidRuntime(9691):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-03 14:43:46.314:E / AndroidRuntime(9691):at dalvik.system.NativeStart.main(Native Method) 07-03 14:43:46.314:E / AndroidRuntime(9691):引起:java.lang.NullPointerException 07-03 14:43:46.314:E / AndroidRuntime(9691):at com.XXX.onCreate(Impostazioni.java:56) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.Activity.performCreate(Activity.java:5104) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 07-03 14:43:46.314:E / AndroidRuntime(9691):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 07-03 14:43:46.314:E / AndroidRuntime(9691):... 11 more
答案 0 :(得分:1)
你的问题在这里getBaseContext() 将其替换为:
intent = new Intent(YourMainActivity.this, YourTargetedActivity.class);
info.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference)
{
intent = new Intent(Impostazioni.this, Informazioni.class));
startActivity(intent);
return true;
}
});