我有一个奇怪的问题,即开始一项活动。我可以使用
从菜单中启动它Intent intent = new Intent(this,SplashScreen.class);
this.startActivity(intent);
但如果我尝试从ListView.setOnItemClickListener启动它,它会崩溃。然而,我尝试了所有类型的方法,结果是相同的(我想开始的活动是一个空白的屏幕,所以没有什么可疑的)。 这是当前的代码......
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startActivity(new Intent(getBaseContext(), SplashScreen.class));
}});
活动被添加到清单......主类扩展了活动。
LogCat
02-23 17:17:11.070: W/dalvikvm(3200): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-23 17:17:11.248: E/AndroidRuntime(3200): FATAL EXCEPTION: main
02-23 17:17:11.248: E/AndroidRuntime(3200): java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.pca.rentme/ro.pca.rentme.SplashScreen}: java.lang.NullPointerException
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.os.Looper.loop(Looper.java:137)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-23 17:17:11.248: E/AndroidRuntime(3200): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 17:17:11.248: E/AndroidRuntime(3200): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 17:17:11.248: E/AndroidRuntime(3200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-23 17:17:11.248: E/AndroidRuntime(3200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 17:17:11.248: E/AndroidRuntime(3200): at dalvik.system.NativeStart.main(Native Method)
02-23 17:17:11.248: E/AndroidRuntime(3200): Caused by: java.lang.NullPointerException
02-23 17:17:11.248: E/AndroidRuntime(3200): at ro.pca.rentme.SplashScreen.onCreate(SplashScreen.java:46)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.Activity.performCreate(Activity.java:5008)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-23 17:17:11.248: E/AndroidRuntime(3200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-23 17:17:11.248: E/AndroidRuntime(3200): ... 11 more
答案 0 :(得分:0)
您不需要getBaseContext()
使用您的活动名称。
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(YourActivityName.this, SplashScreen.class));
}
});
确保清单上的两项活动:
<application ...>
<activity android:name="your.package.name.MainActivity" /> <!-- or .MainActivity -->
<activity android:name="your.package.name.SplashScreen" />
</application>