Android Checkbox错误"在空对象引用"

时间:2018-05-14 09:19:15

标签: android checkbox reference null

我试图创建一个复选框,当它被选中时,它将删除listview中的文本字段元素。我很确定我做的一切都正确,包括为XML中的复选框设置ID,并在我想要使用的类上声明它,但我仍然得到Null对象错误。

我已经查看了所有问题并回答了这个主题,我也尝试了所有问题,但我仍然遇到错误。有人可以帮我确定错误的位置。

提前感谢您的任何帮助=)

类文件

package com.puyakul.prin.psychic_shopping;

import...

public class MainListView extends AppCompatActivity {

    //================DATABASE=====================//
    private ShoppingListDatabase databaseConnection;
    private SQLiteDatabase db;


    public ListView ListView_mainListView;
    public ArrayList<ShoppingList> lists;
    public AppCompatCheckBox checkBox_doneCheck;
    public TextView textView_listname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //================DATABASE Connection================//
        databaseConnection = new ShoppingListDatabase(this);
        db = databaseConnection.open();

        //important!!!
        setContentView(R.layout.activity_main_list_view);
        getLists();

    }

    public void getLists(){

      ....
        ******Original here, but I delete by acident******
        checkBox_doneCheck = findViewById(R.id.checkBox_doneCheck);


        checkBox_doneCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (checkBox_doneCheck.isChecked()) {
                    textView_listname.setPaintFlags(textView_listname.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                }
                else{

                    textView_listname.setPaintFlags(textView_listname.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
        });
    }

XML文件

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/checkBox_doneCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:checked="false"/>

错误日志

Process: com.puyakul.prin.psychic_shopping, PID: 19958
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.puyakul.prin.psychic_shopping/com.puyakul.prin.psychic_shopping.MainListView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatCheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatCheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at com.puyakul.prin.psychic_shopping.MainListView.getLists(MainListView.java:107)
        at com.puyakul.prin.psychic_shopping.MainListView.onCreate(MainListView.java:67)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

2 个答案:

答案 0 :(得分:0)

您错过了xmljava代码之间的关联。 添加以下行:

checkBox_doneCheck = findViewById(R.id.checkBox_doneCheck)

这就是为什么你有NullPointerException

答案 1 :(得分:0)

您永远不会初始化checkBox_doneCheck,请在onCreate中尝试此操作:

setContentView(R.layout.activity_main_list_view);
checkBox_doneCheck = (AppCompatCheckBox) findViewById(R.id.checkBox_doneCheck);
getLists();

您可能还想对这些变量做类似的事情:

  • ListView_mainListView
  • textView_listnameist item