2012-10-05 32 views
1

在我的應用程序通過longCLick我用這種方法創建AlertDialog:的Android的EditText在onCreateDialog

 @Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_TEXT_ENTRY: 
     // This example shows how to add a custom layout to an AlertDialog 
     LayoutInflater factory = LayoutInflater.from(this); 
     final View textEntryView = factory.inflate(R.layout.custom_dialog, null); 
     final EditText cdet11 = (EditText) findViewById(R.id.cdet1); 
     final TextView tv11 = (TextView) findViewById(R.id.buname1); 
     return new AlertDialog.Builder(Main.this) 
      .setIcon(R.drawable.icon) 
      .setTitle("Title") 
      .setView(textEntryView) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        android.os.Debug.waitingForDebugger(); 
        String string1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname11", ""); 
        String ss1 = cdet11.getText().toString(); 
        getSharedPreferences("PREFERENCE", MODE_PRIVATE) 
        .edit() 
        .putString("butname11", ss1) 
        .commit(); 
        String string11 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname11", ""); 
        tv11.setText(string11); 
        /* User clicked OK so do some stuff */ 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked cancel so do some stuff */ 
       } 
      }) 
      .create(); 
    } 
    return null; 
} 

,但我採取這樣的錯誤...

10-05 14:54:25.319: E/AndroidRuntime(6331): java.lang.NullPointerException 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at com.home.Main$3.onClick(Main.java:166) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at android.os.Looper.loop(Looper.java:144) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at java.lang.reflect.Method.invoke(Method.java:521) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-05 14:54:25.319: E/AndroidRuntime(6331):  at dalvik.system.NativeStart.main(Native Method) 

錯誤的

String ss1 = cdet11.getText().toString(); 
occures

有什麼問題?

回答

3

添加視圖參照這些,

final EditText cdet11 = (EditText) findViewById(R.id.cdet1); 
     final TextView tv11 = (TextView) findViewById(R.id.buname1); 

這樣,

final EditText cdet11 = (EditText)textEntryView. findViewById(R.id.cdet1); 
     final TextView tv11 = (TextView)textEntryView. findViewById(R.id.buname1); 

只有當您提供您的要素,可以發現Android將知道到哪裏尋找的看法。

如果不是,它會查看您的活動的ContentView,並會拋出NPE。

+0

謝謝,它的工作原理,但我的TextView的是在主要的onCreate,因此有必要將其傳送到主... – timonvlad

+0

很高興它幫助你解決你的問題..繼續。 :) –

3

使用這樣

private void CreateAlertDialog (final String type) {  

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Title"); 
    alert.setMessage("Message"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      String value = input.getText().toString(); 

     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

     } 
    }); 
    alert.show(); 

} 
+0

我需要在我的自定義佈局膨脹,但謝謝... – timonvlad