我有一個設置屏幕,您可以在其中選擇,添加和刪除應用程序的配置。創建其他類的對象
當添加一個配置時,我創建了一個inputBox類的新實例(擴展設置活動類 - 我存儲標準android文本輸入框的過程)以查詢新配置的名稱。
在此輸入框的Onclick中,將調用superClass(設置活動)中的過程以創建新的配置對象。 此過程從活動(例如選定的微調元素)查詢一些事情,包括seekBar的進度。
這是我得到一個NPE:
java.lang.NullPointerException: Attempt to invoke virtual method'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
同一個對象的創建過程也被稱爲上的應用程序的初始化和工作得很好。
我從Error瞭解到,問題是當從子類調用過程時,變量的引用不再被設置 - 因此不能被查詢。
所以問題:
如何從另一個類調用過程時查詢活動元素的值?
我知道這個話題相當廣泛,但我現在無法弄清楚現在幾天
感謝您的幫助提前。
這是問題的一個方案:
public class Settings extends AppCompatActivity{
Context settingsContext = this;
private Spinner someSpinner;
//other elements
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
someSpinner = (Spinner) findViewById(R.id.someView);
//other elements
addNewConfig.setOnClickListener((v) --> {
inputBox inputBox = new inputBox("OK", "Cancel", settingsContext, "sourcePath",1,1);
newConfigName = inputBox.show();
});
public sSetting makeNewConfig(String name, String sourcePath, int dataFrom, int dataTo){
sSetting newConfig;
newConfig = new sSetting("NAME", someSpinner.getProgress()>0, ...);
return newConfig;
}
}
而且輸入框:
public final class inputBox extends Settings {
//someVars
inputBox(String buttonOk, String buttonCancel, Context setContext, String sourcePath, int dataFrom, int dataTo){
//variable setters
}
private String show() {
//show msgbox
//onclick ok
super.makeNewConfig(....);
}
的構成要素也只包含數據,但我必須從設置屏幕上查詢數據(例如關閉/打開音樂seekbar:我在配置對象中保存true或false) –
'Settings擴展AppCompatActivity','inputBox extends Settings','new inputBox( ...)** ......你不應該這樣做** ......閱讀一些官方指南,嘗試瞭解如何使用Activity和其他Android組件...... – Selvin
什麼是活動的狀態這是阿爾lowed? –