2016-08-19 79 views
1

我對Android非常陌生,並且對我的代碼有個小問題。 在我的代碼中,我有不同的類。 LayoutHelper是將存儲所有需要的信息的對象。 ViewSettings是一類屬於活動activity_view_settings。然後我在ViewSettings.onCreate()初始化我的CheckBoxes,但是當我嘗試在另一個函數ViewSettings.initCheckBox()中獲得檢查狀態時,我得到一個NullPointerException。 我在做什麼錯?Android/Java CheckBox即使在初始化時也會返回null

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)  
+0

它說**行號83 **,在** initCheckBox()**中。請標記你的行號83.我們需要chk。 – Pawan

回答

0

你爲什麼調用onStop()initCheckBox();?這就是當這個活動被永久停止的時候,它在那個時候沒有看法,所以你不能在那裏初始化你的觀點。

移動所有初始化onCreatesetContentView()

+0

我認爲在他的'initCheckBox()'中,他將檢查狀態寫入'SharedPreference' ...他的視圖應該已經在'onCreate()'中初​​始化了。它確實感到奇怪,他在'MainActivity'的'static'字段中初始化它們。 – Shaishav

+0

那真是令人困惑。當它沒有初始化時調用'init':D我放棄了這篇文章。 – Vucko

+0

我認爲這是存儲數據最簡單的方法,因爲當調用'onStop()'時,因爲檢查狀態不再有變化。我應該使用一個OnClickListener來代替... – Packfacker

相關問題