Android 不幸的是<;应用程序名称>;已停止&;显示错误消息";在崩溃报告中包含Java异常堆栈“;在logcat中
使用启动屏幕和progressbar创建webview时,请在MainActivity.java中输入如下代码Android 不幸的是<;应用程序名称>;已停止&;显示错误消息";在崩溃报告中包含Java异常堆栈“;在logcat中,android,android-webview,Android,Android Webview,使用启动屏幕和progressbar创建webview时,请在MainActivity.java中输入如下代码 package com.shopence.myapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.Win
package com.shopence.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://shopence.com/");
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(false);
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.action_settings).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "shopence.com");
intent.putExtra(Intent.EXTRA_TEXT,"Welcome To Shopence");
return intent;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在具有代码的activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/progressbar_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="161dp"
android:indeterminate="false"
android:max="500"
android:progress="0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Loading data..." />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#8FBC8F" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="@android:color/transparent"
android:divider="#00000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling"
android:smoothScrollbar="false" >
</ListView>
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
mShareActionProvider
未正确初始化。它为空,因此为NPE
mShareActionProvider.setShareIntent(getDefaultShareIntent());
在这一行中,您将获得一个NULL:更正此项,以便正确获得shareActionProvider
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.action_settings).getActionProvider();
在Logcat中运行该程序后,显示错误,如
E/SysUtils: ApplicationContext is null in ApplicationStatus
01-04 11:32:28.599 1996-1996/com.shopence.myapp W/chromium: [WARNING:resource_bundle.cc(285)] locale_file_path.empty()
01-04 11:32:29.139 1996-1996/com.shopence.myapp E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
01-04 11:32:29.151 1996-1996/com.shopence.myapp E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
01-04 11:32:29.161 1996-1996/com.shopence.myapp E/chromium: [ERROR:gl_surface_egl.cc(327)] No suitable EGL configs found.
01-04 11:32:29.161 1996-1996/com.shopence.myapp E/chromium: [ERROR:gl_surface_android.cc(23)] GLSurfaceEGL::InitializeOneOff failed.
01-04 11:32:29.165 1996-1996/com.shopence.myapp E/chromium: [ERROR:browser_main_loop.cc(698)] GLSurface::InitializeOneOff failed
01-04 11:32:29.316 1996-1996/com.shopence.myapp E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
01-04 11:32:29.560 1996-2006/com.shopence.myapp W/art: Suspending all threads took: 8.744ms
01-04 11:32:29.578 1996-2151/com.shopence.myapp W/AudioManagerAndroid: Requires BLUETOOTH permission
01-04 11:32:29.902 1996-1996/com.shopence.myapp W/art: Attempt to remove non-JNI local reference, dumping thread
01-04 11:32:29.996 1996-1996/com.shopence.myapp W/AwContents: onDetachedFromWindow called when already detached. Ignoring
01-04 11:32:30.887 1996-2012/com.shopence.myapp I/art: Background sticky concurrent mark sweep GC freed 7248(504KB) AllocSpace objects, 4(100KB) LOS objects, 44% free, 2MB/3MB, paused 4.956ms total 226.065ms
01-04 11:32:30.895 1996-2012/com.shopence.myapp W/art: Suspending all threads took: 7.480ms
01-04 11:32:32.551 1996-2149/com.shopence.myapp W/chromium: [WARNING:data_reduction_proxy_config.cc(423)] SPDY proxy OFF at startup
01-04 11:32:32.987 1996-1996/com.shopence.myapp I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
01-04 11:32:33.415 1996-1996/com.shopence.myapp I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
01-04 11:32:36.872 1996-2006/com.shopence.myapp W/art: Suspending all threads took: 262.669ms
01-04 11:32:37.319 1996-1996/com.shopence.myapp I/Choreographer: Skipped 81 frames! The application may be doing too much work on its main thread.
01-04 11:32:37.670 1996-2200/com.shopence.myapp A/chromium: [FATAL:gl_surface_android.cc(58)] Check failed: kGLImplementationNone != GetGLImplementation() (0 vs. 0)
01-04 11:32:39.665 1996-1996/com.shopence.myapp I/Choreographer: Skipped 89 frames! The application may be doing too much work on its main thread.
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: Chrome build fingerprint:
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: 1.0
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: 1
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: 023f32a1-4862-425f-a49b-e5b1792614ed
01-04 11:32:39.924 1996-2200/com.shopence.myapp W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
01-04 11:32:39.925 1996-2200/com.shopence.myapp E/chromium: ### WebView Version 44.0.2403.119 (code 246011900)
01-04 11:32:39.927 1996-2200/com.shopence.myapp A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 2200 (GpuThread)
您是否在R.menu.menu\u main中添加了android:actionProviderClass,如下图所示
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
...
</menu>
您的共享意图有问题-->mShareActionProvider.setShareContent(getDefaultShareIntent());选中此复选框-->那么我如何解决与该代码相关的问题?在该代码中,mShareActionProvider在顶部全局声明,而您没有…那么我如何解决该问题,即在全局级别删除ShareActionProvider或初始化为localdeclare
private ShareActionProvider mShareActionProvider代码>有关示例,请参阅本页:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
...
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return true;
}