当我按下工具栏中的打印按钮时,我的应用程序关闭了。我想我已将代码放在错误的位置?也许代码错了?或其他什么......
MainActivity.java:
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url ="http://dehvb.nl/";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.loadUrl(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Button btnSimple = (Button) findViewById(R.id.action_about);
btnSimple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), about.class);
startActivityForResult(intent, 0);
}
});
//noinspection SimplifiableIfStatement
if (id == R.id.action_about) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
about.java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
logcat的:
06-12 11:53:10.441 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/WebViewFactory: Loading com.google.android.webview version 50.0.2661.86 (code 266108600)
06-12 11:53:10.441 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/ResourcesManager: getTopLevelResources: /data/app/com.google.android.webview-2/base.apk / 1.0 running in com.vvhvb.hesselfeenstra.vvheerenveenseboys rsrc of package com.google.android.webview
06-12 11:53:10.451 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ResourcesManager: For user 0 new overlays fetched Null
06-12 11:53:10.451 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: Inside getClassLibPath caller
06-12 11:53:10.461 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_LibraryLoader: Time to load native libraries: 2 ms (timestamps 9936-9938)
06-12 11:53:10.461 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_LibraryLoader: Expected native library version number "50.0.2661.86", actual native library version number "50.0.2661.86"
06-12 11:53:10.471 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {bde8c1c}
06-12 11:53:10.471 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_LibraryLoader: Expected native library version number "50.0.2661.86", actual native library version number "50.0.2661.86"
06-12 11:53:10.471 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/chromium: [INFO:library_loader_hooks.cc(143)] Chromium logging enabled: level = 0, default verbosity = 0
06-12 11:53:10.501 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_BrowserStartup: Initializing chromium process, singleProcess=true
06-12 11:53:10.511 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/ApkAssets: Error while loading asset assets/natives_blob_64.bin: java.io.FileNotFoundException: assets/natives_blob_64.bin
06-12 11:53:10.511 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/ApkAssets: Error while loading asset assets/snapshot_blob_64.bin: java.io.FileNotFoundException: assets/snapshot_blob_64.bin
06-12 11:53:10.521 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: loaded /vendor/lib/egl/libGLES_mali.so
06-12 11:53:10.551 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xffccd144
06-12 11:53:10.611 15392-15527/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_media: Requires BLUETOOTH permission
06-12 11:53:10.631 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>
06-12 11:53:10.631 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>
06-12 11:53:10.681 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback>
06-12 11:53:10.691 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback>
06-12 11:53:10.711 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
06-12 11:53:10.731 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
06-12 11:53:10.731 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
06-12 11:53:10.791 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_Ime: ImeThread is not enabled.
06-12 11:53:10.801 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/Activity: performCreate Call Injection manager
06-12 11:53:10.801 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOnViewCreated > Target : com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity isFragment :false
06-12 11:53:10.811 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-12 11:53:10.811 15392-15572/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
06-12 11:53:10.811 15392-15572/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xde341864
06-12 11:53:10.831 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
06-12 11:53:10.831 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xde43fc54
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: Initialized EGL, version 1.4
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: HWUI protection enabled for context , &this =0xf4f12490 ,&mEglDisplay = 1 , &mEglConfig = -185306468
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Enabling debug mode 0
06-12 11:53:10.891 15392-15571/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1
06-12 11:53:11.101 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchCreateOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-12 11:53:11.101 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchPrepareOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-12 11:53:11.271 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3253f19b time:6070740
06-12 11:53:11.501 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 15392
06-12 11:53:11.511 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
06-12 11:53:11.511 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
06-12 11:53:16.391 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
06-12 11:53:16.471 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity
06-12 11:53:16.481 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/AndroidRuntime: Shutting down VM
06-12 11:53:16.481 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vvhvb.hesselfeenstra.vvheerenveenseboys, PID: 15392
java.lang.ClassCastException: android.support.v7.view.menu.ActionMenuItemView cannot be cast to android.widget.Button
at com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity.onOptionsItemSelected(MainActivity.java:48)
at android.app.Activity.onMenuItemSelected(Activity.java:3201)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10604)
at android.view.View$PerformClick.run(View.java:21256)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
06-12 11:53:19.991 15392-15392/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Process: Sending signal. PID: 15392 SIG: 9
about.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
tools:showIn="@layout/activity_main">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我希望有人能帮我解决这个问题。
答案 0 :(得分:0)
您在这一行中获得了一个Class Cast Exception:
Button btnSimple = (Button) findViewById(R.id.action_about);
这意味着您尝试将不属于Button
的内容投射到Button
。在您的情况下,您有ActionMenuItemView
,而不是Button
。
通过强制转换为正确的类型来解决此问题:
ActionMenuItemView btnSimple = (ActionMenuItemView) findViewById(R.id.action_about);
使用onClickListener
更改逻辑,请查看ActionMenuItemView
有哪些方法。
答案 1 :(得分:0)
如果id属于您的menu.xml,那么您应该执行以下操作:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu. menu_main, menu);
}
现在没有用到setOnClickListener,只需使用:
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_about){
Intent intent = new Intent(getContext(), about.class);
startActivityForResult(intent, 0);
}
return super.onOptionsItemSelected(item);
}
希望它对您有用:)
答案 2 :(得分:0)
"filterButton_Pressed:"
如上所述,您无法将操作栏项目强制转换为按钮。 所以删除它:
android.support.v7.view.menu.ActionMenuItemView cannot be cast to android.widget.Button
并将其放在onCreate方法中。 如果它仍然崩溃,则再次发布logcat。它应该改变。 如果您尝试添加菜单项,请按照以下教程进行操作:https://developer.android.com/training/appbar/index.html
答案 3 :(得分:-1)
将您的按钮代码放在OnCreate方法而不是optionmenu