java NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.CheckBox.setOnClickListener,仅在平板电脑上”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36293241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:13:53  来源:igfitidea点击:

NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener on a null object reference, ONLY on tablets

javaandroid

提问by Carrie

I saw many similar questions online, and tried several solutions. But none fixed my issue. One important thing I want to mention is that, my code works well on android Phones, it only crashes on android Tablets. I have no idea what's going on.

我在网上看到很多类似的问题,并尝试了几种解决方案。但没有人解决我的问题。我想提的一件重要事情是,我的代码在 android 手机上运行良好,它只在 android 平板电脑上崩溃。我不知道是怎么回事。

This is the stack:

这是堆栈:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx.android/com.xxxx.xxxx.android.LoginScreenActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access0(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.xxxx.xxxx.android.LoginScreenActivity.onCreate(LoginScreenActivity.java:89)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
... 10 more

This is the LoginScreenActivity:

这是登录屏幕活动:

public class LoginScreenActivity extends Activity implements OnClickListener
{
    private Context context;
    private CheckBox showPassword;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);

        context = getApplicationContext();
        showPassword = (CheckBox)findViewById(R.id.checkbox_show_password);

        //below is line 89 of LoginScreenActivity, exception happened here.
        showPassword.setOnClickListener(new OnClickListener()
        {
          @Override
          public void onClick(View view)
          {
            //.................
          }
        });
    }
}

Here's the xml file:

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/checkbox_show_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Show Password"/>
    </RelativeLayout>

</ScrollView>

回答by OneCricketeer

There are two problems that are apparent.

有两个问题很明显。

The obvious: NullPointerException

显而易见的: NullPointerException

The solution: All instances of login_screen.xmlwill need a Checkboxwith android:id="@+id/checkbox_show_password"in order for findViewById(R.id.checkbox_show_password)to not return null.

解决方案: 的所有实例login_screen.xml都需要一个Checkboxwithandroid:id="@+id/checkbox_show_password"才能findViewById(R.id.checkbox_show_password)不返回空值。

This is worth mentioning because in the comments it was stated that there are multiple res/layout-*folders and the error only happens on tablets, so that is important.

这是值得一提的,因为在评论中指出有多个res/layout-*文件夹并且错误只发生在平板电脑上,所以这很重要。

Second problem:

第二个问题:

While any Viewcan use an OnClickListener, you have a CheckBox, not a Button, so you'll probably want to know when the checkbox was checked, not just clicked.

虽然任何人View都可以使用 an OnClickListener,但您使用的是CheckBox,而不是Button,因此您可能想知道复选框何时被选中,而不仅仅是单击。

In that case, use an OnCheckedChangeListener

在这种情况下,请使用 OnCheckedChangeListener

showPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            // do something
        }else {
            //do something else
        }
    }
});

回答by Kostas Drak

Well with a checkbox you cannot have an onClickListener. Please remove the Interface (implements) also. You may want to try this:

好吧,有了复选框,你就不能有一个onClickListener. 也请移除接口 ( implements)。你可能想试试这个:

showPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (showPassword.isChecked()){
                    //do something
                }else {
                    //do something else
                }
            }
        });

Hope it helps!!!

希望能帮助到你!!!