2 hcg 21 hcg_21 于 2013.04.23 11:46 提问

onCreateDialog 中的 EditText

在程序中通过 longCLick 创建了AlertDialog

  @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;
}

但是获得这样的错误:

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

问题出在哪里呢?

1个回答

luhuajcdd
luhuajcdd   2013.04.23 15:56

错误信息不够全.
这里有怎么创建一个Dialog:http://developer.android.com/intl/zh-CN/guide/topics/ui/dialogs.html

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color);
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    builder.create();
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!