我有一个简单的Android应用程序,带有WebView加载我的网站,最近我将这个项目从Windows XP迁移到Mac,从那时起我在模拟器中运行App时调用findViewById()时出现异常,我试过运行在移动设备中没有任何问题。
错误消息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampl/com.exampl.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.webkit.WebView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5026)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.webkit.WebView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:57)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:307)
at android.app.Activity.setContentView(Activity.java:1930)
at com.exampl.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5242)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 24 more
Caused by: java.lang.UnsupportedOperationException
at com.android.webview.nullwebview.NullWebViewFactoryProvider.createWebView(NullWebViewFactoryProvider.java:41)
at android.webkit.WebView.ensureProviderCreated(WebView.java:2058)
at android.webkit.WebView.setOverScrollMode(WebView.java:2116)
at android.view.View.<init>(View.java:3461)
at android.view.View.<init>(View.java:3517)
at android.view.ViewGroup.<init>(ViewGroup.java:470)
at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
at android.webkit.WebView.<init>(WebView.java:498)
at android.webkit.WebView.<init>(WebView.java:475)
at android.webkit.WebView.<init>(WebView.java:455)
at android.webkit.WebView.<init>(WebView.java:444)
... 27 more
MainActivity.java源代码
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
Log.e("joecmt", R.layout.activity_main+" ");
setContentView(R.layout.activity_main);
Log.e("joecmt", R.id.activity_main_webview+"");
Log.e("joecmt", findViewById(R.id.activity_main_webview)!=null?"nnull":"null");
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath("");
mWebView.loadUrl("http://m.example.com/");
}
的AndroidManifest.xml
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity"
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 0 :(得分:2)
确保您的模拟器具有互联网访问权限。例如我的防火墙阻止了: sdk / tools / emulator-x86.exe
答案 1 :(得分:0)
您需要在AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
试试这个。