我刚接触Android,对我的代码有一个小问题。
在我的代码中,我有不同的类。 LayoutHelper是将存储所有所需信息的对象。 ViewSettings是一个属于Activity activity_view_settings的类。然后,在CheckBoxes中初始化我的ViewSettings.onCreate(),但是当我尝试在另一个函数ViewSettings.initCheckBox()中获取检查状态时,我得到了NullPointerException
我究竟做错了什么?

类ViewSettings:

public class ViewSettings extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_settings);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
    }

    MainActivity.layoutHelpers[0].setCheckBox((CheckBox) findViewById(R.id.checkBox_MCC));
    MainActivity.layoutHelpers[1].setCheckBox((CheckBox) findViewById(R.id.checkBox_CID));
    MainActivity.layoutHelpers[2].setCheckBox((CheckBox) findViewById(R.id.checkBox_LAC));
    MainActivity.layoutHelpers[3].setCheckBox((CheckBox) findViewById(R.id.checkBox_BS));
    MainActivity.layoutHelpers[4].setCheckBox((CheckBox) findViewById(R.id.checkBox_location));
    MainActivity.layoutHelpers[5].setCheckBox((CheckBox) findViewById(R.id.checkBox_tech));
    MainActivity.layoutHelpers[6].setCheckBox((CheckBox) findViewById(R.id.checkBox_pci));

    for(int i=0;i<MainActivity.numberOfObjects;i++){
        MainActivity.layoutHelpers[i].getCheckBox().setChecked(MainActivity.layoutHelpers[i].getIsVisible());
    }
}

public void initCheckBox(){
    SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.FILENAME,0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(MainActivity.VAL_KEY,1);

    for(int i=0;i<MainActivity.numberOfObjects;i++){
        editor.putBoolean(MainActivity.layoutHelpers[i].getName(),MainActivity.layoutHelpers[i].getCheckBox().isChecked());
    }
    editor.commit();

    for(int i=0;i<MainActivity.numberOfObjects;i++){
        MainActivity.layoutHelpers[i].initHelper(sharedPreferences);
    }
}

public static void initTextViews(){
    for(int i=0;i<MainActivity.layoutHelpers.length;i++){
        if(MainActivity.layoutHelpers[i].getIsVisible()){
            MainActivity.layoutHelpers[i].getTextView_num().setVisibility(TextView.VISIBLE);
            MainActivity.layoutHelpers[i].getTextView_text().setVisibility(TextView.VISIBLE);
        }else{
            MainActivity.layoutHelpers[i].getTextView_num().setVisibility(TextView.GONE);
            MainActivity.layoutHelpers[i].getTextView_text().setVisibility(TextView.GONE);
        }
    }
}

@Override
protected void onStop() {
    initCheckBox();
    initTextViews();
    super.onStop();
}


LayoutHelper类:

public class LayoutHelper {

public TextView textView_num, textView_text;
public boolean isVisible;
public CheckBox checkBox;
public String name;

public LayoutHelper(TextView textView_num, TextView textView_text,     SharedPreferences sharedPreferences, String name){
    this.textView_num = textView_num;
    this.textView_text = textView_text;
    this.name = name;
    this.isVisible = sharedPreferences.getBoolean(name,true);
    this.checkBox = null;
}

public TextView getTextView_num(){return textView_num;}
public TextView getTextView_text(){return textView_text;}
public boolean getIsVisible(){return isVisible;}
public CheckBox getCheckBox(){return checkBox;}
public String getName(){return name;}
public void setVisible(boolean isVisible){this.isVisible =isVisible;}
public void setCheckBox(CheckBox checkBox){this.checkBox = checkBox;}
public void setName(String name) {this.name = name;}
public void initHelper(SharedPreferences sharedPreferences){
    this.isVisible = sharedPreferences.getBoolean(this.name,true);
}
}


我的XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.e103046.sitef_v2.ViewSettings"
    android:id="@+id/content">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:id="@+id/view"
        android:background="#00c6d7">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_view_settings" />
    <ExpandableListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expandableListView"
        android:layout_below="@+id/checkBox_tech"
        android:layout_alignParentStart="true" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/MCCMNC"
        android:id="@+id/checkBox_MCC"
        android:clickable="true"
        android:checked="true"
        android:enabled="true"
        android:focusable="true"
        android:layout_below="@+id/view"
        android:layout_alignParentStart="true"
        android:layout_marginStart="43dp"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CID"
        android:id="@+id/checkBox_CID"
        android:checked="true"
        android:clickable="true"
        android:enabled="true"
        android:layout_below="@+id/checkBox_MCC"
        android:layout_alignStart="@+id/checkBox_MCC"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/LAC"
        android:id="@+id/checkBox_LAC"
        android:enabled="true"
        android:checked="true"
        android:clickable="true"
        android:layout_below="@+id/checkBox_CID"
        android:layout_alignStart="@+id/checkBox_CID"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Basestation"
        android:id="@+id/checkBox_BS"
        android:layout_below="@+id/checkBox_LAC"
        android:layout_alignStart="@+id/checkBox_LAC"
        android:enabled="true"
        android:checked="true"
        android:clickable="true"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Location"
        android:id="@+id/checkBox_location"
        android:layout_below="@+id/checkBox_BS"
        android:layout_alignStart="@+id/checkBox_BS"
        android:checked="true"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Technology"
        android:id="@+id/checkBox_tech"
        android:layout_below="@+id/checkBox_location"
        android:layout_alignStart="@+id/checkBox_location"
        android:checked="true"
        android:textSize="20dp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/PCI"
        android:id="@+id/checkBox_pci"
        android:layout_below="@+id/checkBox_tech"
        android:layout_alignStart="@+id/checkBox_tech"
        android:checked="true"
        android:textSize="20dp" />
    </RelativeLayout>


日志:

Process: com.example.e103046.sitef_v2, PID: 8478
    java.lang.RuntimeException: Unable to stop activity {com.example.e103046.sitef_v2/com.example.e103046.sitef_v2.ViewSettings}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4150)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4213)
        at android.app.ActivityThread.access$1500(ActivityThread.java:177)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5938)
        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:1400)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference
        at com.example.e103046.sitef_v2.ViewSettings.initCheckBox(ViewSettings.java:83)
        at com.example.e103046.sitef_v2.ViewSettings.onStop(ViewSettings.java:123)
        at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1275)
        at android.app.Activity.performStop(Activity.java:6492)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4145)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4213) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:177) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:5938) 
        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:1400) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

最佳答案

为什么在initCheckBox();中呼叫onStop()?那是活动被永久停止并且那时没有视图的时候,所以您不能在那里初始化视图。

将所有初始化移至onCreate之后的setContentView()中。

10-06 05:39