2 hxn 217 hxn_217 于 2013.01.28 13:22 提问

什么错误会使应用程序崩溃呢?

当我点击应用程序的logo,闪屏后,就会从intent中调用的第一个类。但是当tab加载后, onPreExecute() 一旦执行,应用程序就奔溃了。

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

                }

        }

在 doInBackground() 函数处得到错误。
确切的错误:

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

问题出在哪呢?

2个回答

yongyong_21
yongyong_21   2013.01.29 10:11
已采纳

你正在试图实现的功能在一个后台线程中包括 UI(ListView lv = (ListView)

findViewById(R.id.user_list);)

你可以在后台处理信息,然后传回UI线程,更新UI.

cytown
cytown   2013.01.29 10:41

lv.setAdapter(csl);这个是在ui线程执行的东西,要放在其他地方,比如onProgressUpdate或者onPostExecute里面。

或者自己创建handler去发消息给这个handler让他进行处理。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!