0

This is my code. When I click on my app logo, after the splash screen, this is the class that is first called from an intent. But, after the tab is loaded, and onPreExecute() is once executed, the app crashes.

public class HomeActivity extends Activity{
    private static final String dialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_main_tab_home);

        new HomeDownloadPage().execute();
    }


    public class HomeDownloadPage extends AsyncTask<String,Void,String>{

        private final ProgressDialog dialog = new ProgressDialog(HomeActivity.this);




         protected void onPreExecute() {
             this.dialog.setMessage("Have Paitence! ");
             this.dialog.show();


          }

         @Override
            protected String doInBackground(String... params) {

                User user = null;

                 try {


                        user = new User("4eeb");
                        user.getList();

                        /*
                         * Custom adapter
                         * */
                        ArrayList<User> users = new ArrayList<User>();

                        for(User u : user.following){
                            users.add(u);
                        }

                        ListView lv = (ListView) findViewById(R.id.user_list);

                        final UserFollowingListAdapter csl = new UserFollowingListAdapter(HomeActivity.this,R.layout.user_list,users,this);


                        OnItemClickListener listener = new OnItemClickListener() {

                            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

                            Object o = csl.getItem(position);


                            setTitle(parent.getItemAtPosition(position).toString());
                            }
                          };

                          lv.setAdapter(csl);


                          lv.setOnItemClickListener(listener);


                          /*
                           * Onclick listener
                           * */     
                          lv.setOnItemClickListener(new OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                                            Intent i = new Intent("com.list.SEARCH");
                                            Toast.makeText(HomeActivity.this, "rowitem clicked", Toast.LENGTH_LONG).show();
                                            // TODO Auto-generated method stub

                                        }
                                    });

                        } catch (Exception e) {

                                showError();
                        }
                return null;
            }

         protected void onPostExecute(String result) {
                // execution of result of Long time consuming operation

              }

    }

                public void showError(){
                    new AlertDialog.Builder(HomeActivity.this)
                    .setTitle(" Oops , Server down :( ")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub

                        }
                        //
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // Do nothing.
                        }
                    }).show();

                }




        }

Error I get is at the doInBackground() function.

Exact error: 01-19 19:03:01.264: E/AndroidRuntime(1138): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

What is the problem?

Dalmas
  • 26,409
  • 9
  • 67
  • 80
Hick
  • 35,524
  • 46
  • 151
  • 243
  • Looks similar to http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare – MikeTheReader Jan 19 '12 at 20:07

2 Answers2

1

You are attempting to do things involving the UI (ListView lv = (ListView) findViewById(R.id.user_list);) within a background thread. You can not do this. You may process information in the background, then pass it back to the UI thread and update the UI

Ashterothi
  • 3,282
  • 1
  • 21
  • 35
1

As pyrodante said, you're attempting to modify the UI while not on the UI thread. If you want modify the UI from a non-UI thread, you can use the runOnUiThread() function. That said, there's a better solution to your problem. You really should be using a Loader. They were basically designed to address exactly what you're trying to do. Note that even if you're designing an app that's pre-3.0, you can still access loaders via the Android Support package.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102