2017-05-27 76 views
0

我正在構建一個在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(); 
    } 

} 
+0

請發表您的onCreate方法 – lelloman

+0

move'final EditText et_semester =(EditText)findViewById(R.id.et_semester);'OnCreate'方法 –

+0

@lelloman它在那裏。 –

回答

0

你的EditText R.id .et_semester不在您的活動內容視圖中,因此您必須在對話視圖中找到此視圖

// Inflate your view 
    View dialogView = LayoutInflater.from(context).inflate(R.layout.custom_layout, null); 
    // Bind views 
    final EditText et_semester = (EditText) dialogView.findViewById(R.id.et_semester); 
    // Create dialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(context) 
      .setTitle("Create A Semester") 
      .setView(dialogView) 
      .setCancelable(false); 
+0

我在我的'addItems()'方法中初始化dialogView嗎? –

+0

@OnurOzbek是的,應該工作 –

0

漢克穆迪的回答是正確的。這就是我所做的。

public void addSemester(View v){ 

     View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_layout, null); 
     final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester); 

     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
     builder.setTitle("Create A Semester"); 
     builder.setView(dialogView); 
     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(); 
    } 

調用方法addSemester()onCreate()是不是一個好主意,因爲在該方法中的某些設置,通過FAB點擊觸發的,但如果你將其放置在onCreate()方法,它們運行的​​瞬間活動創建。

相關問題