0

アプリケーション内の設定の取得と設定を処理する PreferenceActivity クラスがあります。しかし、起動時にWebサービスを呼び出す「メイン」Activityクラスがあり、Webサービスからの戻り値に基づいて、設定の1つを更新する必要があります。

NullPointerException エラーをスローせずにこれを機能させることはできないようです。

メイン アクティビティのコードは次のとおりです。

protected void onPostCreate(Bundle savedInstanceState)
{

    if(getWebServiceResult.equals("FALSE"))
    {
      //do stuff
    }
    else
    {
       myPreferences prefs = new myPreferences();               
       prefs.updateMyChoice("TRUE");
    }
}

エラーをスローしている PreferenceActivity クラスのコードは次のとおりです。

public void updateMyChoice(String newText)
{
    if(subscriber_opt_in == null) //this is coming up null
    {
       subscriber_opt_in = (EditTextPreference) findPreference("opt_in"); //error here
    }
   subscriber_opt_in.setText(newText);
   subscriber_opt_in.setSummary(newText);
}

この設定を適切に更新する方法を知る必要があります。メインの Activity クラス内でそれを行う方法があれば、それはさらに良いことですが、PreferenceActivity クラスを通じてそれを行う必要がある場合は、その方法を理解する必要があるだけです。

ありがとう!

4

1 に答える 1

0

SharedPreference クラスを使用する必要があります: http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

コード例と使用方法: http://developer.android.com/guide/topics/data/data-storage.html#pref

于 2012-04-23T21:44:12.017 に答える