当我按下操作栏中的按钮时,我的应用程序确实会崩溃,但只有当按钮与 web 视图有关时才会发生这种情况。但是 webview 加载网页的 onCreate 方法仍然有效。
public boolean onOptionsItemSelected(MenuItem item) {
setContentView(R.layout.activity_main);
mWebview =(WebView) findViewById(R.id.webView1);
switch (item.getItemId()) {
// If button 1 is pressed
case R.id.about:
// Toast Message
Toast.makeText(this, "App made by Robin Flikkema & Sander ten Hove",
Toast.LENGTH_LONG).show();
break;
// If Share is pressed
case R.id.menu_share:
// Toast message
Toast.makeText(this, "Roosterwijzigingen delen",
Toast.LENGTH_SHORT).show();
// Making share selector
String shareBody = "Er vervallen nieuwe lessen! Check het uit, met de Eemsdeltacollege Roosterwijzigingen App.";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Eemsdeltacollege Roosterwijzigingen");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
break;
// If Next is pressed
case R.id.menu_next:
// Toast message
Toast.makeText(this, "Morgen",
Toast.LENGTH_SHORT).show();
// Calculate Date
Calendar cal=Calendar.getInstance();
int currentDay=cal.get(Calendar.DAY_OF_MONTH);
//Set the date to 1 day
cal.set(Calendar.DAY_OF_MONTH, currentDay+1);
while (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal.add(Calendar.DATE, 1);
}
Date DateDate = cal.getTime();
SimpleDateFormat postFormater = new SimpleDateFormat("dd-M-yyyy");
String newDateStr = postFormater.format(DateDate);
// Set URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php?datum="+newDateStr);
setContentView(mWebview );
break;
// If Refresh is pressed
case R.id.menu_refresh:
// Toast message
Toast.makeText(this, "Roosterwijzigingen ophalen.",
Toast.LENGTH_SHORT).show();
// Load URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
setContentView(mWebview );
break;
// If Overmorgen is pressed
case R.id.menu_nextt:
// Toast message
Toast.makeText(this, "Overmorgen",
Toast.LENGTH_SHORT).show();
// Calculate Date
Calendar cal1=Calendar.getInstance();
int currentDay1=cal1.get(Calendar.DAY_OF_MONTH);
//Set the date to 1 day
cal1.set(Calendar.DAY_OF_MONTH, currentDay1+2);
while (cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal1.add(Calendar.DATE, 1);
}
Date DateDate1 = cal1.getTime();
SimpleDateFormat postFormater1 = new SimpleDateFormat("dd-M-yyyy");
String newDateStr1 = postFormater1.format(DateDate1);
// Set URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php?datum="+newDateStr1);
setContentView(mWebview );
break;
// If Help is pressed
case R.id.action_settings:
// Toast message
Toast.makeText(this, "Laden van de Help pagina.",
Toast.LENGTH_SHORT).show();
// Load Help
mWebview .loadUrl("https://dl.dropboxusercontent.com/u/53488707/RoosterwijzigingenApp/index.html");
setContentView(mWebview );
break;
// If Vandaag is pressed
case R.id.menu_back:
// Toast message
Toast.makeText(this, "Vandaag",
Toast.LENGTH_SHORT).show();
// Load Help
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
setContentView(mWebview);
break;
}
return super.onOptionsItemSelected(item);
}
日志猫
06-25 16:04:12.495: E/AndroidRuntime(16692): FATAL EXCEPTION: main
06-25 16:04:12.495: E/AndroidRuntime(16692): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.view.ViewGroup.addView(ViewGroup.java:3249)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.view.ViewGroup.addView(ViewGroup.java:3225)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:275)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:265)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.app.Activity.setContentView(Activity.java:1887)
06-25 16:04:12.495: E/AndroidRuntime(16692): at org.flikkema.robin.dagrooster.MainActivity.onOptionsItemSelected(MainActivity.java:138)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.app.Activity.onMenuItemSelected(Activity.java:2534)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.view.View.performClick(View.java:4084)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.view.View$PerformClick.run(View.java:16966)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.os.Handler.handleCallback(Handler.java:615)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.os.Looper.loop(Looper.java:137)
06-25 16:04:12.495: E/AndroidRuntime(16692): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-25 16:04:12.495: E/AndroidRuntime(16692): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 16:04:12.495: E/AndroidRuntime(16692): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-25 16:04:12.495: E/AndroidRuntime(16692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-25 16:04:12.495: E/AndroidRuntime(16692): at dalvik.system.NativeStart.main(Native Method)
LogCat 说了一些关于 removeview()