HI 我在整个应用程序中使用选项卡为应用程序工作。在一个特定的选项卡中,集成了facebook和twitter.its 单击facebook按钮后,它显示了facebook的登录屏幕,登录成功也工作正常,但是当登录完成时,我想重定向到另一个活动,我在onComplete() 中使用以下代码
View view = Socialtab.group.getLocalActivityManager()
.startActivity("Items", new Intent(Social.this,MainActivity.class)
.putExtra("name", name)
.putExtra("email", email)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
Socialtab.group.replaceView(view);
它在下一行显示异常
.startActivity("Items", new Intent(Social.this,MainActivity.class)
我的日志猫错误如下:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.karmakexpo/com.karmakexpo.MainActivity}: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1524)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at com.karmakexpo.Social.jsonParsing(Social.java:353)
at com.karmakexpo.Social$SampleRequestListener.onComplete(Social.java:258)
at com.karmakexpo.facebook.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:238)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Activity.<init>(Activity.java:698)
at com.karmakexpo.MainActivity.<init>(MainActivity.java:56)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409) at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
... 6 more
我已经用谷歌搜索过,没有得到任何解决方案,请给出解决方案提前谢谢..
尝试使用runOnUiThread:
Social.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
View view = Socialtab.group.getLocalActivityManager()
.startActivity("Items", new Intent(Social.this,MainActivity.class)
.putExtra("name", name)
.putExtra("email", email)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
Socialtab.group.replaceView(view);
}
});
您需要在主 UI 线程中运行代码。
试试这个
Looper.prepare(); // Initialize the current thread as a looper. This gives you a chance to create handlers that then reference this looper, before actually starting the loop.
View view = Socialtab.group.getLocalActivityManager()
.startActivity("Items", new Intent(Social.this,MainActivity.class)
.putExtra("name", name)
.putExtra("email", email)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
Socialtab.group.replaceView(view);
Looper.loop();