2016-08-23 65 views
-2

我一直在努力讓它可以保存用戶輸入到自定義對話框EditText中的任何數據,但沒有成功。該應用程序只是在具有getText()函數的線路上每次崩潰。任何幫助將深表感謝。自定義對話框中的getText

public void comments(View v){ 

    AlertDialog.Builder builder2; 

    builder2 = new AlertDialog.Builder(this); 
    // Get the layout inflater 
    LayoutInflater inflater = getLayoutInflater(); 

    builder2.setTitle("Recipe Comments"); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    final View view = inflater.inflate(R.layout.fragment_comments, null); 

    builder2.setView(view) 
      // Add action buttons 
      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // sign in the user ... 
        EditText text = (EditText)view.findViewById(R.id.comments); 
        String temp = text.getText().toString(); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //LoginDialogFragment.this.getDialog().cancel(); 
       } 
      }); 
    builder2.create(); 
    builder2.show(); 
} 

這裏是蹤跡:

08-22 23:43:38.111 10837-10897/com.example.wesle.wsuuioption1 I/OpenGLRenderer:初始化EGL,版本1.4 08- 22 23:43:43.568 10837-10837/com.example.wesle.wsuuioption1 D/AndroidRuntime:關閉虛擬機 08-22 23:43:43.571 10837-10837/com.example.wesle.wsuuioption1 E/AndroidRuntime:FATAL EXCEPTION :main 進程:com.example.wesle.wsuuioption1,PID:10837 java.lang.Null PointerException:試圖調用虛方法 'android.text.Editable android.widget.EditText.getText()'null object reference at com.example.wesle.wsuuioption1.MainActivity $ 7.onClick(MainActivity.java: 2936) at android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:157) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop (Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

+0

只是把它張貼您的logcat讓人們可以找到你做了什麼錯吧.... – sushildlh

+0

只是貼吧,謝謝。 –

+0

請檢查您的'R.layout.fragment_comments'編輯文本的id是否爲'comments'或張貼您的xml文件 –

回答

0

製作在設置視圖之前連接她,並使其成爲最終的。

public void comments(View v){ 

    AlertDialog.Builder builder2; 

    builder2 = new AlertDialog.Builder(this); 
    // Get the layout inflater 
    LayoutInflater inflater = getLayoutInflater(); 

    builder2.setTitle("Recipe Comments"); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    final View view = inflater.inflate(R.layout.fragment_comments, null); 

    // Make connection her before setting view. 
    final EditText text = (EditText)view.findViewById(R.id.comments); 

    builder2.setView(view) 
      // Add action buttons 
      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // sign in the user ... 
        //EditText text = (EditText)view.findViewById(R.id.comments); 
        String temp = text.getText().toString(); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //LoginDialogFragment.this.getDialog().cancel(); 
       } 
      }); 
    builder2.create(); 
    builder2.show(); 
} 
+0

依然在我身上發生了變化 –

+0

發佈了你的'fragment_comments'代碼。 –

0

您無法通過views獲取組件。從DialogInterfaceobject

public void comments(View v){ 

    AlertDialog.Builder builder2; 

    builder2 = new AlertDialog.Builder(this); 
    // Get the layout inflater 
    LayoutInflater inflater = getLayoutInflater(); 

    builder2.setTitle("Recipe Comments"); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    final View view = inflater.inflate(R.layout.fragment_comments, null); 


    builder2.setView(view) 
      // Add action buttons 
      .setPositiveButton("Save", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // sign in the user ... 
        EditText text = (EditText)dialog.findViewById(R.id.comments); 
        String temp = text.getText().toString(); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //LoginDialogFragment.this.getDialog().cancel(); 
       } 
      }); 
    builder2.create(); 
    builder2.show(); 
}