我有一个名为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>
答案 0 :(得分:2)
试试这个:
尝试移动声明
lv.setAdapter(new LazyAdapter1(ListControlActivity.this, z));
从线程到Handler
创建一个处理程序:
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
lv.setAdapter(new LazyAdapter1(ListControlActivity.this, z));
}
};
并用handler的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();
答案 1 :(得分:0)
您无法从UI线程以外的任何线程更新列表视图(或任何其他视图)。使用AsyncTask代替普通帖子,并确保更新onPostExecute()
中的列表视图。