定义AlertDialog未在runonuithread方法中显

定义AlertDialog未在runonuithread方法中显

这是代码...请让我知道我在哪里出错了..我也在将适配器设置为listview的适配器,该视图在膨胀的v1版本中...这都是在smartfox服务器的扩展响应中完成的...然后在UI Thread上执行以下过程。

 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)

最佳答案

您必须首先以相反的顺序进行更改。

 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);

关于java - 自定义AlertDialog未在runonuithread方法中显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20680967/

10-09 03:28