我有一个在C中运行的线程,当某些条件满足时,它将调用我的UI线程(
Java)静态方法.调用静态方法时,我希望在我的UI上显示Toast.我试过的是:
1
static void myMethod(){
Toast.makeText(context, "message", Toast.LENGTH_SHORT).show();
(I have a static context reference in global scope)
}
结果:
E/AndroidRuntime( 1331): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
2
static void myMethod(){
runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(Context, "message", Toast.LENGTH_SHORT).show();
}
});
结果:
Can not compile: Cannot make a static reference to the non-static method runOnUiThread(Runnable) from the type Activity
有人可以对此有所了解吗?非常感谢你.