2016-08-19 8 views
1

私はAndroidにはかなり新しく、自分のコードについて少し質問があります。 私のコードでは、私は異なるクラスを持っています。 LayoutHelperは、すべての再入荷された情報を格納するオブジェクトです。 ViewSettingsはアクティビティactivity_view_settingsに属するクラスです。 CheckBoxesViewSettings.onCreate()に初期化しますが、別の関数でチェック状態を取得しようとすると、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

** initCheckBox()**の** line number 83 **と書かれています。あなたの行番号83に印をつけてください。私たちはchkする必要があります。 – Pawan

答えて

0

なぜあなたはonStop()initCheckBox();を呼んでいますか?それはアクティビティが永久に停止されていて、その時点でビューがないため、そこでビューを初期化できません。

onCreateのすべての初期化をsetContentView()の後に移動します。

+0

彼の 'initCheckBox()'では 'SharedPreference'にチェック状態を書いていると思います。彼のビューは' onCreate() 'で既に初期化されているはずです。 'MainActivity'の' static'フィールドで初期化しているのは変です。 – Shaishav

+0

それはちょうど混乱している。初期化されていないときに 'init'を呼び出す:D私はこの投稿をあきらめます。 – Vucko

+0

データを保存する最も簡単な方法だと思いました。なぜなら、 'onStop()'が呼ばれると、チェックされた状態にそれ以上の変更がないからです。私はおそらく代わりにOnClickListenerを使用する必要があります... – Packfacker

関連する問題