2016-08-23 7 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(); 
} 

Вот это StackTraces:

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: выключение VM 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()' на ссылки объекта нулевой в com.example.wesle.wsuuioption1.MainActivity $ 7.onClick (MainActivity.java: 2936) на android.support.v7.app.AlertController $ ButtonHandler.handleMessage (AlertController.java:157) в android.os.Handler.dispatchMessage (Handler.java:102) в android.os.Looper.loop (Looper.java:148) at android.app.ActivityThread.main (ActivityThread.java:5417) at java.lang.reflect.Method.invoke (собственный метод) в 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' edittext' comments' или разместите свой xml-файл –

ответ

0

Make подключите ее перед настройкой просмотра и сделайте ее окончательной.

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(); 
} 
Смежные вопросы