NullPointerException异常的setOnClickListener [英] NullPointerException in setOnClickListener

查看:255
本文介绍了NullPointerException异常的setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很无奈我和没有其他问题解决了我的问题。

我使用一个测试应用程序从一本书,并复制所有code一字不差。一切都很好更早运行,但是当我试图让我自己的风格,它开始崩溃。所以,我解开了一切,删除样式的XML文件,它仍然崩溃。

会发生什么事是我点击应用,出现了应用程序的背景,但没有我添加上来的按钮;相反,它说:不幸的是,(应用程序名称)已停止。

控制台没有给我任何问题,但这里的LogCat中所说的:

  20 12-29:45:29.346:D / AndroidRuntime(2606):关闭VM
12-29 20:45:29.346:W / dalvikvm(2606):主题ID = 1:螺纹未捕获的异常(组= 0x409c01f8)退出
12-29 20:45:29.394:E / AndroidRuntime(2606):致命异常:主要
12-29 20:45:29.394:E / AndroidRuntime(2606):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.recipesapp / com.recipesapp.MainMenu}:显示java.lang.NullPointerException
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.os.Looper.loop(Looper.java:137)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread.main(ActivityThread.java:4424)
12-29 20:45:29.394:E / AndroidRuntime(2606):在java.lang.reflect.Method.invokeNative(本机方法)
12-29 20:45:29.394:E / AndroidRuntime(2606):在java.lang.reflect.Method.invoke(Method.java:511)
12-29 20:45:29.394:E / AndroidRuntime(2606):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
12-29 20:45:29.394:E / AndroidRuntime(2606):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-29 20:45:29.394:E / AndroidRuntime(2606):在dalvik.system.NativeStart.main(本机方法)
12-29 20:45:29.394:E / AndroidRuntime(2606):因:显示java.lang.NullPointerException
12-29 20:45:29.394:E / AndroidRuntime(2606):在com.recipesapp.MainMenu.onCreate(MainMenu.java:16)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.Activity.performCreate(Activity.java:4465)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-29 20:45:29.394:E / AndroidRuntime(2606):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-29 20:45:29.394:E / AndroidRuntime(2606):11 ...更多
12-29 20:45:35.215:I /流程(2606):发送信号。 PID:2606 SIG:9

我认识的唯一一个是显示java.lang.NullPointerException在com.recipesapp.MainMenu.onCreate(MainMenu.java:16)所以我看准了这一点。而事实证明,当我注释掉线16那里,应用精细运行;不过,我想不通为什么它造成的一个问题。

下面是产生错误的code,用16行说。

 公共类MainMenu的扩展活动实现OnClickListener {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
       查看aboutButton = findViewById(R.id.main_about_button);
        aboutButton.setOnClickListener(本); // 16行
    }    公共无效的onClick(查看thisView){
        开关(thisView.getId()){
            案例R.id.main_about_button:
                意图showAbout =新意图(这一点,About.class);
                startActivity(showAbout);
                打破;
        }
    }
}

这里是清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.recipesapp
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =15/>    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@android:风格/ Theme.Light>
        <活动
            机器人:名字=的MainMenu
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;        <活动
            机器人:关于NAME =
            机器人:主题=@安卓风格/ Theme.Dialog>        < /活性GT;
    < /用途>
< /清单>

和布局的code / main.xml中有问题的按钮:

 <按钮
        机器人:ID =@ + ID / about_button
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / exit_button
        机器人:layout_alignLeft =@ + ID / exit_button
        机器人:文字=@字符串/ main_about_button/>


解决方案

问题是你混了按钮的名称。

Java的:

 查看aboutButton = findViewById(R.id.main_about_button);

XML:

 机器人:ID =@ + ID / about_button

要么修改第二个 @ + ID / main_about_button 还是先 R.id.about_button ,你应该是好去。

This is really frustrating me and none of the other questions solved my problem.

I'm using a test app from a book, and copied all the code verbatim. Everything was running fine earlier, but when I tried to make my own style, it started crashing. So I undid everything and deleted the XML file for the style, and it's still crashing.

What happens is I click on the app, the background for the app appears, but none of the buttons I added come up; instead, it says "Unfortunately, (appname) has stopped."

The console isn't giving me any problems, but here's what LogCat says:

12-29 20:45:29.346: D/AndroidRuntime(2606): Shutting down VM
12-29 20:45:29.346: W/dalvikvm(2606): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-29 20:45:29.394: E/AndroidRuntime(2606): FATAL EXCEPTION: main
12-29 20:45:29.394: E/AndroidRuntime(2606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.recipesapp/com.recipesapp.MainMenu}: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.os.Looper.loop(Looper.java:137)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at java.lang.reflect.Method.invoke(Method.java:511)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at dalvik.system.NativeStart.main(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606): Caused by: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.recipesapp.MainMenu.onCreate(MainMenu.java:16)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.Activity.performCreate(Activity.java:4465)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-29 20:45:29.394: E/AndroidRuntime(2606):     ... 11 more
12-29 20:45:35.215: I/Process(2606): Sending signal. PID: 2606 SIG: 9

The only one I recognized was thejava.lang.NullPointerException at com.recipesapp.MainMenu.onCreate(MainMenu.java:16) so I zeroed in on that. And it turns out that when I comment out line 16 there, the app runs fine; however, I can't figure out why it's causing a problem.

Here is the error-generating code, with line 16 noted.

public class MainMenu extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       View aboutButton = findViewById(R.id.main_about_button);
        aboutButton.setOnClickListener(this);    //LINE 16
    }

    public void onClick(View thisView) {
        switch (thisView.getId()) {
            case R.id.main_about_button:
                Intent showAbout = new Intent(this, About.class);
                startActivity(showAbout);
                break;
        }
    }
}

And here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.recipesapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme = "@android:style/Theme.Light" >
        <activity
            android:name=".MainMenu"
            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=".About"
            android:theme="@android:style/Theme.Dialog">

        </activity>
    </application>
</manifest>

And the code in layout/main.xml for the button in question:

 <Button
        android:id="@+id/about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/exit_button"
        android:layout_alignLeft="@+id/exit_button"
        android:text="@string/main_about_button" />

解决方案

The problem is you're mixing up the name of the Button.

Java:

View aboutButton = findViewById(R.id.main_about_button);

XML:

android:id="@+id/about_button"

Either change the second to @+id/main_about_button or the first to R.id.about_button and you should be good to go.

这篇关于NullPointerException异常的setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆