简体   繁体   English

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

[英]Why is onClickListener crashing my app when I start it?

i'm a new programmer trying to load a new view when I push a button on my splash screen. 我是一名新程序员,当我在启动屏幕上按一个按钮时,试图加载一个新视图。 My splash is a viewPager with 4 views. 我最喜欢的是一个具有4个视图的viewPager。 I have a button on those views which when pushed I want a listview view to load. 我在那些视图上有一个按钮,按下这些按钮时我希望加载一个listview视图。 But When I added onClickListener it crashes as soon as the app starts. 但是,当我添加onClickListener时,它会在应用启动后立即崩溃。 I've been trying to figure this out for a few days now and now i'm really desperate! 我已经尝试了几天,现在我真的很绝望! Thanks in advance. 提前致谢。

I tried to find out what fatal exception MAIN means, but it seems be a different problem for every program. 我试图找出致命异常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"));
        }
    });
}
}

//Crash Log //崩溃日志

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

//manifest //表现

<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布局

<?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>

I just tried to add the android:onClick and it's still crashing 我只是尝试添加android:onClick而仍然崩溃

Your problem is this: 您的问题是这样的:

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)

If I were to guess (as you have not pasted the full source for your activity, I am not sure which line is line 40), I'd say your bug is on this line: 如果我猜测(由于您尚未粘贴活动的完整源代码,我不确定哪一行是第40行),我想说您的错误就在这行上:

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

Specifically, your layout most likely is missing <ImageButton android:id="@+id/splashB" ...> element. 具体来说,您的布局很可能缺少<ImageButton android:id="@+id/splashB" ...>元素。 Thus, your listButton is null, which results in the NullPointerException in the next line. 因此,您的listButton为null,这将在下一行中导致NullPointerException

Update: Your app crashes because, while it does have a <Button> with an id of splashB , that button is not an <ImageButton> , which is what your code expects. 更新:您的应用程序崩溃是因为,尽管它确实有一个ID为splashB<Button> ,但该按钮不是 <ImageButton> ,这正是您的代码所期望的。

You have to either change your layout to use <ImageButton> or change this line: 您必须更改布局以使用<ImageButton>或更改以下行:

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

to: 至:

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

Either should work. 两者都应该起作用。

I might be guessing but i think your actual problem lies in this piece of code 我可能正在猜测,但我认为您的实际问题出在这段代码中

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

Here you are starting a new Activity with an action, But you are not giving a valid Action . 在这里,您将使用一个动作开始一个新的Activity ,但是您没有提供有效的Action So if you want to start a new activity there You should pass the Context and your valid class name . 因此,如果要在此处开始新活动,则应传递Contextyour valid class name Or if you are starting an Activity with an Action please make sure you are starting a valid action . 或者,如果您要通过Action启动“ Activity Action请确保您正在执行valid action

Please look at Starting Another Activity , and this Start Activity Using Custom Action . 请查看“ 开始另一个活动” ,以及“ 使用自定义操作开始活动”

listButton = (ImageButton) findViewById(R.id.splashB); listButton =(ImageButton)findViewById(R.id.splashB); here it's not reffering the correct id .According to my guess either splashB reffering to something else other than image button or its not created. 这里没有引用正确的id。根据我的猜测,splashB引用了除图像按钮以外的其他东西或未创建。 So check inside xml file or paste your xml such that i can conclude the main reasons behind the error. 因此,请检查xml文件内部或粘贴您的xml,以便我可以得出错误背后的主要原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM