0

我有一个像这样的名为 ListApp 的应用程序

public class ListApp extends Application {

public static RSmovies[] appRsMovie=new RSmovies[1];
public static ImageDownloader id =new ImageDownloader();
public static String cur=null;
public static String movieGetInfo="http://hive.tn/TMDb/test11.php?param=";

}

在我的第一个活动中,我尝试在新线程中对 appRsMovie 进行修改,如下所示

new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                InputStream source = retrieveStream("http://hive.tn/TMDb/test6.php");
                Gson gson = new Gson();

                Reader reader = new InputStreamReader(source);
                    ListApp.appRsMovie = gson.fromJson(reader, RSmovies[].class);

                lv
                        .setAdapter(new LazyAdapter1(
                                ListControlActivity.this, z));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

应用程序崩溃,这是我得到的例外

12-26 17:21:55.643: W/dalvikvm(958): Exception Ljava/lang/RuntimeException; thrown during Lcom/test/list/ListApp;.<clinit>
4

2 回答 2

2

尝试这个:

尝试移动语句

lv.setAdapter(new LazyAdapter1(ListControlActivity.this, z));

从线程到处理程序

创建一个处理程序:

    Handler handler = new Handler() {

    public void handleMessage(android.os.Message msg) {
        lv.setAdapter(new LazyAdapter1(ListControlActivity.this, z));
    }
};

并用处理程序的 sendMessage 替换 lv.setAdapter 语句

new Thread(new Runnable() {
    @Override
    public void run() {

        try {
            InputStream source = retrieveStream("http://hive.tn/TMDb/test6.php");
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);
                ListApp.appRsMovie = gson.fromJson(reader, RSmovies[].class);

            handler.sendEmptyMessage(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).start();
于 2011-12-26T16:01:16.870 回答
0

您不能从 UI 线程以外的任何线程更新列表视图(或任何其他视图)。使用AsyncTask而不是普通线程,并确保在onPostExecute().

于 2011-12-26T17:34:14.427 回答