我是一名新程序员,尝试在按下启动屏幕上的按钮时加载新 View 。我的启动项是一个具有 4 个 View 的 viewPager。我在这些 View 上有一个按钮,按下该按钮时我想要加载 ListView View 。但是当我添加 onClickListener 时,应用程序启动后它就会崩溃。这几天我一直在试图解决这个问题,现在我真的很绝望!提前致谢。
我试图找出致命异常 MAIN 的含义,但似乎每个程序都有不同的问题。
public class Splash extends Activity {
ImageButton listButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
SplashPager adapter = new SplashPager();
ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
listButtonListener();
}
public void listButtonListener() {
listButton = (ImageButton) findViewById(R.id.splashB);
listButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent("com.example.survtest1.Main"));
}
});
}
}
//崩溃日志
01-14 22:12:32.069: W/dalvikvm(14733): threadid=1: thread exiting with uncaught exception (group=0x40207560)
01-14 22:12:32.079: E/AndroidRuntime(14733): FATAL EXCEPTION: main
01-14 22:12:32.079: E/AndroidRuntime(14733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest1/com.example.survtest1.Splash}: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.os.Looper.loop(Looper.java:130)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-14 22:12:32.079: E/AndroidRuntime(14733): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): at java.lang.reflect.Method.invoke(Method.java:507)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-14 22:12:32.079: E/AndroidRuntime(14733): at dalvik.system.NativeStart.main(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.example.survtest1.Splash.listButtonListener(Splash.java:40)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.example.survtest1.Splash.onCreate(Splash.java:31)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-14 22:12:32.079: E/AndroidRuntime(14733): ... 11 more
01-14 22:12:32.079: E/AndroidRuntime(14733): [Blue Error Handler] Make Debugging Report file for main
01-14 22:12:32.079: E/AndroidRuntime(14733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest1/com.example.survtest1.Splash}: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.os.Looper.loop(Looper.java:130)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.main(ActivityThread.java:3733)
01-14 22:12:32.079: E/AndroidRuntime(14733): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): at java.lang.reflect.Method.invoke(Method.java:507)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-14 22:12:32.079: E/AndroidRuntime(14733): at dalvik.system.NativeStart.main(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.example.survtest1.Splash.listButtonListener(Splash.java:40)
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.example.survtest1.Splash.onCreate(Splash.java:31)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-14 22:12:32.079: E/AndroidRuntime(14733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-14 22:12:32.079: E/AndroidRuntime(14733): ... 11 more
// list
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.survtest1.Splash"
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="com.example.survtest1.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
//xml布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="@+id/splash"
android:background="#000000" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/intro__1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaaa"
android:layout_gravity="bottom"
android:layout_marginLeft="75dp"
android:id="@+id/splashB"
android:onClick="startActivity" />
</FrameLayout>
我刚刚尝试添加 android:onClick 但它仍然崩溃
最佳答案
您的问题是这样的:
01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733): at com.example.survtest1.Splash.listButtonListener(Splash.java:40)
如果我猜测(因为您没有粘贴 Activity 的完整源代码,我不确定哪一行是第 40 行),我会说您的错误在这一行:
listButton = (ImageButton) findViewById(R.id.splashB);
具体来说,您的布局很可能丢失 <ImageButton android:id="@+id/splashB" ...>
元素。因此,您的listButton
为 null,结果为 NullPointerException
在下一行。
更新:您的应用程序崩溃是因为,虽然它确实有 <Button>
ID 为splashB
,该按钮不是 <ImageButton>
,这就是您的代码所期望的。
您必须更改布局才能使用 <ImageButton>
或更改此行:
listButton = (ImageButton) findViewById(R.id.splashB);
至:
listButton = (Button) findViewById(R.id.splashB);
两者都应该有效。
关于java - 为什么 onClickListener 在我启动应用程序时会使它崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14330606/