当我启动它时,为什么onClickListener会崩溃我的应用程序?

时间:2013-01-15 03:16:13

标签: java android xml android-viewpager onclicklistener

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

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

3 个答案:

答案 0 :(得分: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,但该按钮 { {1}},这是您的代码所期望的。

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

<ImageButton>

为:

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

要么应该工作。

答案 1 :(得分:0)

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

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

在这里,您开始使用操作新的Activity,但您没有提供有效的Action。因此,如果您想在那里开始新的活动,您应该通过Contextyour valid class name。或者,如果您使用Activity开始Action,请确保您开始使用valid action

请查看Starting Another ActivityStart Activity Using Custom Action

答案 2 :(得分:0)

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