0

私はいくつかを更新しようとしていTextViewsます。Main_Activity.XMLデフォルトPortraitとの3 つのレイアウトがありますLandscape。これらのTextViewレイアウトの はすべて同じですandoid:id

の更新についてできることはすべて読みましたがTextView、これがアプリ全体をクラッシュさせる理由がわかりません。

TextView txt = (TextView) findViewById(R.id.txtViewActiveTimeProp1)おそらくnullを返していると思いますが、しようとするsetTextとnullポインタ例外が発生します。何かご意見は?私は入れました

TextView txt = (TextView) findViewById(R.id.txtViewActiveTimeProp1);
String strPref = retrievePrefs("pref_active_times_prop1");
txt.setText(strPref); 

上記と同じ結果で が完了してButton onclickからかなり後にイベントが発生した場合。3 つのレイアウトすべてで出口setContentViewを参照するために使用している ID であることを確認しました。TextView

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PreferenceManager.setDefaultValues(this, R.xml.preference, false);
    setContentView(R.layout.activity_main);

TextView txt = (TextView) findViewById(R.id.txtViewActiveTimeProp1);
String strPref = retrievePrefs("pref_active_times_prop1");
txt.setText(strPref);
}
4

2 に答える 2

1

こんにちは onCreate メソッド内で findViewById を実行する必要があります。イベントリスナー内でfindViewByIdを実行しているため、nullが返されます。

コードで以下の変更を行います。

public class MainActivity extends Activity {
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

PreferenceManager.setDefaultValues(this, R.xml.preference, false);
setContentView(R.layout.activity_main);

txt = (TextView) findViewById(R.id.txtViewActiveTimeProp1);
String strPref = retrievePrefs("pref_active_times_prop1");
txt.setText(strPref);
}

そしてあなたのクリックリスナーの中

String strPref = retrievePrefs("pref_active_times_prop1");
txt.setText(strPref);

お役に立てれば。

于 2012-08-27T12:44:00.600 に答える
0

値 String strPref = retrievePrefs("pref_active_times_prop1"); のようです。来ている null 、その strPref 値を出力してチェックする

于 2012-08-27T12:58:30.977 に答える