AndroidMech
AndroidMech

Reputation: 1696

Custom AlertDialog not displaying in runonuithread method

this is the code...plz let me know where did i go wrong.. also i am setting an adapter to the listview which is conatined in the View v1 inflated...this is all done in the extension response of a smartfox server...and then the following process on 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();
                 }

this is my logcat output..

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)

Upvotes: 0

Views: 167

Answers (1)

Piyush
Piyush

Reputation: 18933

You have to first change this in reverse order.

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

You have to set view after find id of your ListView.

Also don't make adplist variable globally. Make it static.

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

Upvotes: 1

Related Questions