![](/img/trans.png)
[英]NullPointerException when accessing another class in ActionListener
[英]Getting NullPointerException when accessing spinner of another class
我有兩個類Settings
和MainActivity
。 我正在嘗試從MainActivity
類訪問在Settings
類中創建的微調器。
這通常有效
public static Settings getlang = new Settings();
問題是,如果我嘗試這樣做而不首先打開Settings活動並直接進入MainActivity,我會在此行獲得NullPointerException
getlang.getLang1().setSelection(getlang.getLang());
但是,如果我首先打開“ Settings
活動然后轉到MainActivity
一切正常。
我怎樣才能解決這個問題?
這是“ Settings
活動
public class Settings extends Activity {
public SharedPreferences prefsSet;
public String prefNameSet = "MyPrefSet";
public static final String PREFS_NAME_SET = "SAVEDATASET";
private static final String SPINNER1_STATE = "spinner1_state";
public int language;
public int userChoice;
private static Spinner spinner1;
private Button savesett;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
savesett = (Button) findViewById(R.id.bSaveSett);
spinner1 = (Spinner) findViewById(R.id.spinner1);
SharedPreferences sharedPref = getSharedPreferences("FileName",
MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("userChoiceSpinner", -1);
if (spinnerValue != -1)
// set the value of the spinner
spinner1.setSelection(spinnerValue);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
userChoice = spinner1.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",
0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("userChoiceSpinner", userChoice);
prefEditor.commit();
Toast.makeText(
parent.getContext(),
"Chosen Language: "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
savesett.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ourIntent = new Intent(Settings.this, MainActivity.class);
startActivity(ourIntent);
}
});
}
public int getLang() {
return userChoice;}
public Spinner getLang1() {
return Settings.spinner1;
}
}
從您的代碼我可以假設(或可能猜測) 您正在使用Settings.class中的Spinner來選擇用戶的語言,並且您還將它保存在SharedPreferences .xml中。
因此,現在不是從Spinner對象獲取微調器的值,而是從SharedPreferences.xml中讀取它 。 即使您沒有這樣做,我認為這是解決您問題的最佳方式。
現在,當您第一次打開應用程序並且沒有為該語言設置首選項時,您可以設置默認值(例如英語) 。 當用戶從Settings微調器更改語言時,您可以將其保存在SharedPreferences中,然后從MainActivity.class中讀取它 。
使用共享首選項的代碼:
- >將數據寫入共享首選項:
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// Saving Data to Shared Preferences
editor.putInt("userChoiceSpinner", 1);
editor.commit();
- >從共享首選項中讀取數據:
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int spinner_value = prefs.getInt("userChoiceSpinner", -1); // Here -1 is the default value to return when there is no value for the key "userChoiceSpinner" in the Shared Preferences.
通過不創建Spinner的實例, spinner1 = (Spinner) findViewById(R.id.spinner1);
永遠不會跑。
你必須做這樣的事情:
制作方法:
public static void setSpinner(int id){
spinner=findViewById(id);
}
並事先運行它。
您不應該創建活動類的實例
public static Settings getlang = new Settings(); // wrong
活動由具有intent的startActivity啟動。 活動有一個生命周期和生命周期。
findviewById
在當前的膨脹布局中查找id為id的視圖。 因此,如果您嘗試在Settings
中的MainActivity
中進行初始化,則會獲得NullPointerException
。
如果需要spinner中的選定值,則可以使用intent在活動之間傳遞值。
您也可以使用SharedPreferences
僅當特定活動至少啟動一次時才調用onCreate
方法。 但是,由於您甚至沒有啟動Settings
活動”,因此您嘗試訪問的微調器不會被初始化,因為該微調器不會找到您通過findViewByid(id)
提供的findViewByid(id)
。 這就是你得到空指針異常的原因 。
當你在啟動Settings
Activity之后訪問該微調器時,微調器就能找到它提供的id,因為onCreate
方法至少被調用一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.