0

我是一名新程序员,当我在启动画面上按下按钮时,我试图加载新视图。我的启动画面是一个有 4 个视图的 viewPager。我在那些视图上有一个按钮,当按下时我想要加载一个列表视图视图。但是当我添加 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

//显现

<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 并且它仍然崩溃

4

3 回答 3

1

你的问题是这样的:

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)

如果我猜测(因为您没有粘贴活动的完整源代码,我不确定哪一行是第 40 行),我会说您的错误在这一行:

listButton = (ImageButton) findViewById(R.id.splashB);

具体来说,您的布局很可能缺少<ImageButton android:id="@+id/splashB" ...>元素。因此,您的listButton为空,这会导致NullPointerException下一行出现。

更新:您的应用程序崩溃,因为虽然它<Button>的 id 为splashB,但该按钮不是<ImageButton>这是您的代码所期望的。

您必须更改布局以使用<ImageButton>或更改此行:

listButton = (ImageButton) findViewById(R.id.splashB);

至:

listButton = (Button) findViewById(R.id.splashB);

要么应该工作。

于 2013-01-15T03:51:55.403 回答
0

listButton = (ImageButton) findViewById(R.id.splashB); 在这里,它没有引用正确的 id 。根据我的猜测,splashB 引用了除图像按钮以外的其他东西,或者它没有被创建。因此,检查内部 xml 文件或粘贴您的 xml,以便我可以得出错误背后的主要原因。

于 2013-01-15T04:30:08.937 回答
0

我可能在猜测,但我认为您的实际问题在于这段代码

public void onClick(View arg0) {
     startActivity(new Intent("com.example.survtest1.Main"));
}

在这里,您正在开始一个新Activity的动作,但您没有给出有效的Action. 所以如果你想在那里开始一个新的活动你应该通过Contextand your valid class name。或者,如果您开始Activity使用 anAction请确保您开始使用valid action.

请查看开始另一个活动,以及使用自定义操作开始活动

于 2013-01-15T03:34:47.750 回答