我正在構建一個在FAB單擊時顯示警告對話框的應用程序,接受用戶輸入並使用該用戶輸入命名ExpandableListView組標題。當我點擊FAB按鈕並放入文本時,它會給我一個空對象引用。爲什麼我會在空對象引用上獲取EditText.getText()'?
我初始化我的學期前onCreate()
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ExpandableListView expandableListView;
ExpandableListViewAdapter adapter;
private FloatingActionButton addItems;
private String semester = "";
在我addItems()
方法,它是當有FAB點擊調用,我創建我的自定義警告對話框,讓用戶輸入字符串和配售它在我的ArrayList中的適配器。
public void addItems() {
final EditText et_semester = (EditText)findViewById(R.id.et_semester);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Create A Semester");
builder.setView(R.layout.custom_layout);
builder.setCancelable(false);
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
semester = et_semester.getText().toString();
adapter.groupNames.add(semester);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
adapter.notifyDataSetChanged();
builder.show();
}
但是,當我點擊AlertDialog完成按鈕,這是我的堆棧跟蹤:
05-30 05:35:10.295 20943-20943/myapp.onur.expandablelistviewexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp.onur.expandablelistviewexample, PID: 20943
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at myapp.onur.expandablelistviewexample.MainActivity$1.onClick(MainActivity.java:45)
編輯這是我MainActivity
:
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListViewAdapter adapter;
private FloatingActionButton addItems;
private String semester;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expListView);
adapter = new ExpandableListViewAdapter(MainActivity.this);
expandableListView.setAdapter(adapter);
addItems = (FloatingActionButton) findViewById(R.id.add_items);
final EditText et_semester = (EditText)findViewById(R.id.et_semester);
}
public void addSemester(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create A Semester");
builder.setView(R.layout.custom_layout);
builder.setCancelable(false);
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText et_semester = (EditText).findViewById(R.id.et_semester);
semester = etSemester.getText().toString();
adapter.groupNames.add(semester);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
adapter.notifyDataSetChanged();
builder.show();
}
}
請發表您的onCreate方法 – lelloman
move'final EditText et_semester =(EditText)findViewById(R.id.et_semester);'OnCreate'方法 –
@lelloman它在那裏。 –