我正在使用以下方式开始新的活动
Intent edit = new Intent(getParent(), WebViewLink.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
edit.putExtra(POSITION, FAQ);
parentActivity.startChildActivity("WebViewLink", edit);
我收到这样的错误
08-19 13:44:49.955: ERROR/AndroidRuntime(548): Uncaught handler: thread main exiting due to uncaught exception
08-19 13:44:49.974: ERROR/AndroidRuntime(548): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44dabdc8 is not valid; is your activity running?
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.view.ViewRoot.setView(ViewRoot.java:468)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.app.Dialog.show(Dialog.java:239)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at com.stress.WebViewLink$2.onLoadResource(WebViewLink.java:73)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:355)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.os.Looper.loop(Looper.java:123)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): at dalvik.system.NativeStart.main(Native Method)
但是当我使用这种方式来调用新活动时没有问题
Intent edit = new Intent(getParent(), WebViewLink.class);
edit.putExtra(POSITION, FAQ);
More.this.startActivity(edit);
你可以在这里看到我的WebViewLink.java类
public class WebViewLink extends Activity {
public static String POSITION = "position";
int position = 0;
private Button back;
private TextView ttl;
ProgressDialog myProgressDialog = null;
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
back = (Button) findViewById(R.id.backtomore);
ttl = (TextView) findViewById(R.id.my_title);
webView.getSettings().setJavaScriptEnabled(true);
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(POSITION)) {
position = getIntent().getExtras().getInt(POSITION);
}
}
switch (position) {
case More.FAQ:
ttl.setText("FAQ");
setWebLink();
webView.loadUrl("file:///android_asset/faq4.htm");
break;
case More.ABOUT_US:
ttl.setText("About Us");
setWebLink();
webView.loadUrl("file:///android_asset/Advice/About Us.html");
break;
case More.DISCLAIMMER:
ttl.setText("Disclaimer");
setWebLink();
webView.loadUrl("file:///android_asset/Advice/Disclaimer.html");
break;
}
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private void setWebLink() {
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
if (myProgressDialog == null) {
myProgressDialog = new ProgressDialog(WebViewLink.this);
myProgressDialog.setMessage("Loading.... Please Wait");
myProgressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (myProgressDialog.isShowing()) {
myProgressDialog.dismiss();
}
}
});
}
}
答案 0 :(得分:2)
问题在于这行代码:
myProgressDialog = new ProgressDialog(WebViewLink.this);
当你是TabGroupActivity
时,孩子Activity
(ies)需要父母'Context
添加新的Windows,Dialogs。试试这个:
myProgressDialog = new ProgressDialog(WebViewLink.this.getParent());
并对所有Dialog
执行相同操作。
答案 1 :(得分:0)
08-19 13:44:49.974: ERROR/AndroidRuntime(548): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44dabdc8 is not valid; is your activity running?
以上错误表示您的活动未运行。因此,当您使用以下说明调用此活动时,程序会崩溃。因为它无法进行任何活动。
TabGroupActivity parentActivity = (TabGroupActivity) getParent();