【问题标题】:What is the error in this android code that the app crashes? [duplicate]应用程序崩溃的这个android代码中的错误是什么? [复制]
【发布时间】:2012-02-14 11:52:42
【问题描述】:

这是我的代码。当我在启动屏幕之后单击我的应用程序徽标时,这是首先从意图调用的类。但是,在加载选项卡并执行一次 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-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()

有什么问题?

【问题讨论】:

标签: android


【解决方案1】:

您正试图在后台线程中执行涉及 UI (ListView lv = (ListView) findViewById(R.id.user_list);) 的操作。你不能做这个。您可以在后台处理信息,然后将其传递回 UI 线程并更新 UI

【讨论】:

    【解决方案2】:

    正如 pyrodante 所说,您正在尝试修改 UI 而不是在 UI 线程上。如果要从非 UI 线程修改 UI,可以使用 runOnUiThread() 函数。也就是说,您的问题有更好的解决方案。你真的应该使用Loader。它们基本上旨在解决您正在尝试做的事情。请注意,即使您设计的是 3.0 之前的应用,您仍然可以通过 Android Support package 访问加载程序。

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      相关资源
      最近更新 更多