自定义AlertDialog不在runonuithread方法中显示

时间:2013-12-19 11:48:22

标签: java android

这是代码... PLZ让我知道我哪里出错了。我也设置了一个适配器到listview,它在View v1中被包含了...这都是在扩展响应中完成的一个smartfox服务器...然后在UI线程上进行以下过程..

 runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                alert();
                            }

                        });


public void alert(){

            AlertDialog.Builder ab=new AlertDialog.Builder(RegActivity.this);
            LayoutInflater linf=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View v1=linf.inflate(R.layout.proj_list, null);
            ab.setView(v1);
            ab.setTitle("Select a group");
            ListView lvprojlist=(ListView) findViewById(R.id.projdisplaylist);
            adplist=new ArrayAdapter<String>(RegActivity.this, R.layout.list_item, R.id.rowtext, projlist);
            lvprojlist.setAdapter(adplist);


            ab.setCancelable(false).setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();


                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
            ab.create();
            ab.show();
                 }

这是我的logcat输出..

12-19 17:14:33.869: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-19 17:14:33.889: E/AndroidRuntime(699): FATAL EXCEPTION: main
12-19 17:14:33.889: E/AndroidRuntime(699): java.lang.NullPointerException
12-19 17:14:33.889: E/AndroidRuntime(699):  at com.registersfsmodule.RegActivity.alert(RegActivity.java:392)
12-19 17:14:33.889: E/AndroidRuntime(699):  at com.registersfsmodule.RegActivity$3$2.run(RegActivity.java:178)
12-19 17:14:33.889: E/AndroidRuntime(699):  at android.os.Handler.handleCallback(Handler.java:587)
12-19 17:14:33.889: E/AndroidRuntime(699):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 17:14:33.889: E/AndroidRuntime(699):  at android.os.Looper.loop(Looper.java:130)
12-19 17:14:33.889: E/AndroidRuntime(699):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-19 17:14:33.889: E/AndroidRuntime(699):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 17:14:33.889: E/AndroidRuntime(699):  at java.lang.reflect.Method.invoke(Method.java:507)
12-19 17:14:33.889: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-19 17:14:33.889: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-19 17:14:33.889: E/AndroidRuntime(699):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您必须先按相反顺序更改此内容。

 ab.setTitle("Select a group");
 ListView lvprojlist=(ListView) v1.findViewById(R.id.projdisplaylist);
 ab.setView(v1);

您必须在找到 ListView 的ID后设置视图。

此外,请勿全局制作 adplist 变量。让它静止。

ArrayAdapter<String >adplist=new ArrayAdapter<String>(RegActivity.this, R.layout.list_item, R.id.rowtext, projlist);