线程和处理程序在 android 中不起作用

Thread and handler not work in android

我使用loading before load completed listview

此代码有效:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
final ListView listContent = (ListView) findViewById(R.id.listContent);
        adapter = new AdapterNote(G.tasks);
        listContent.setAdapter(adapter);
        Commands.readData();
        adapter.notifyDataSetChanged();
}

但更改为以下代码后,应用程序崩溃并强制关闭:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
startDialog();
}

ProgressDialog pd;


    private void startDialog() {

        pd = ProgressDialog.show(NoteActivity.this, "title", "loading");

        new Thread(new Runnable() {

            @Override
            public void run() {
                final ListView listContent = (ListView) findViewById(R.id.listContent);
        adapter = new AdapterNote(G.tasks);
        listContent.setAdapter(adapter);
        Commands.readData();
        adapter.notifyDataSetChanged();

                handler.sendEmptyMessage(0);
            }
        }).start();
    }

    Handler handler = new Handler() {

                        @Override
                        public void handleMessage(Message msg) {
                            pd.dismiss();
                        }
                    };

在日志 cat 中出现了这些错误:

03-08 00:27:09.734: E/AndroidRuntime(334): FATAL EXCEPTION: main
03-08 00:27:09.734: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NoteProject.download.app/com.NoteProject.download.app.NoteNoteProjectActivity}: java.lang.NullPointerException
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread.access00(ActivityThread.java:125)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 00:27:09.734: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:27:09.734: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:521)
03-08 00:27:09.734: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 00:27:09.734: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 00:27:09.734: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
03-08 00:27:09.734: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
03-08 00:27:09.734: E/AndroidRuntime(334):  at com.NoteProject.download.app.NoteNoteProjectActivity.onScroll(NoteNoteProjectActivity.java:79)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:675)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.widget.AbsListView.setOnScrollListener(AbsListView.java:664)
03-08 00:27:09.734: E/AndroidRuntime(334):  at com.NoteProject.download.app.NoteNoteProjectActivity.onCreate(NoteNoteProjectActivity.java:69)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:27:09.734: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-08 00:27:09.734: E/AndroidRuntime(334):  ... 11 more

如何解决?

commands.readdata() 中,return 来自网络的列表视图

感谢

更新 liatview 是一个 UI 操作,需要在主 (UI) 线程上执行。

adapter.notifyDataSetChanged();必须 运行 在 UI 线程,这将有效:

  private void startDialog() {

    pd = ProgressDialog.show(NoteActivity.this, "title", "loading");
    final ListView listContent = (ListView) findViewById(R.id.listContent);
    adapter = new AdapterNote(G.tasks);
    listContent.setAdapter(adapter);

    new Thread(new Runnable() {

        @Override
        public void run() {

    Commands.readData();

            handler.sendEmptyMessage(0);
        }
    }).start();
}

Handler handler = new Handler() {

                    @Override
                    public void handleMessage(Message msg) {
    adapter.notifyDataSetChanged();
                        pd.dismiss();
                    }
                };

您可以使用异步任务在后台加载列表视图。下面编写的代码对我很好。

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     final ListView listContent = (ListView) findViewById(R.id.listContent);
new AsyncTask<Void, Void, Void>() 
    {
        @Override
        protected void onPreExecute() 
        {
            pd = ProgressDialog.show((NoteActivity.this, "title", "loading", true, false);

        }// End of onPreExecute method

        @Override
        protected Void doInBackground(Void... params) 
        {
        //reading data for listview
        Commands.readData();    
         return null;
        }// End of doInBackground method

        @Override
        protected void onPostExecute(Void result)
        {

            pd.dismiss();

            //setting adapter here
        adapter = new AdapterNote(G.tasks);
    listContent.setAdapter(adapter);}//End of onPostExecute method
    }.execute((Void[]) null);

}