这是我的Logcat。
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit W/dalvikvm﹕ VFY: unable to resolve virtual method 407: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit W/dalvikvm﹕ VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getType (I)I
08-06 21:38:29.646 25786-25786/com.example.ananaybatra.reddit D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-06 21:38:29.802 25786-25786/com.example.ananaybatra.reddit V/WebViewChromiumFactoryProvider﹕ Binding Chromium to main looper Looper (main, tid 1) {41cca600}
08-06 21:38:29.802 25786-25786/com.example.ananaybatra.reddit I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
08-06 21:38:29.802 25786-25786/com.example.ananaybatra.reddit I/chromium﹕ [INFO:library_loader_hooks.cc(116)] Chromium logging enabled: level = 0, default verbosity = 0
08-06 21:38:29.802 25786-25786/com.example.ananaybatra.reddit I/BrowserStartupController﹕ Initializing chromium process, renderers=0
08-06 21:38:29.810 25786-25786/com.example.ananaybatra.reddit E/AudioManagerAndroid﹕ BLUETOOTH permission is missing!
08-06 21:38:29.833 25786-25786/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-06 21:38:29.841 25786-25786/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-06 21:38:29.841 25786-25786/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-06 21:38:30.513 25786-25825/com.example.ananaybatra.reddit W/chromium﹕ [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
08-06 21:38:30.685 25786-25786/com.example.ananaybatra.reddit D/AndroidRuntime﹕ Shutting down VM
08-06 21:38:30.685 25786-25786/com.example.ananaybatra.reddit W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419a0ba8)
08-06 21:38:30.685 25786-25786/com.example.ananaybatra.reddit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ananaybatra.reddit, PID: 25786
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ananaybatra.reddit/com.example.ananaybatra.reddit.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.webkit.WebView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.webkit.WebView
at com.example.ananaybatra.reddit.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
08-06 21:38:33.427 25786-25786/com.example.ananaybatra.reddit I/Process﹕ Sending signal. PID: 25786 SIG: 9
08-06 21:40:44.935 26681-26681/com.example.ananaybatra.reddit D/dalvikvm﹕ Late-enabling CheckJNI
08-06 21:40:45.443 26681-26681/com.example.ananaybatra.reddit I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-06 21:40:45.443 26681-26681/com.example.ananaybatra.reddit W/dalvikvm﹕ VFY: unable to resolve virtual method 407: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-06 21:40:45.451 26681-26681/com.example.ananaybatra.reddit D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-06 21:40:45.451 26681-26681/com.example.ananaybatra.reddit I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-06 21:40:45.451 26681-26681/com.example.ananaybatra.reddit W/dalvikvm﹕ VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getType (I)I
08-06 21:40:45.451 26681-26681/com.example.ananaybatra.reddit D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-06 21:40:45.505 26681-26681/com.example.ananaybatra.reddit V/WebViewChromiumFactoryProvider﹕ Binding Chromium to main looper Looper (main, tid 1) {41cd1598}
08-06 21:40:45.505 26681-26681/com.example.ananaybatra.reddit I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
08-06 21:40:45.513 26681-26681/com.example.ananaybatra.reddit I/chromium﹕ [INFO:library_loader_hooks.cc(116)] Chromium logging enabled: level = 0, default verbosity = 0
08-06 21:40:45.513 26681-26681/com.example.ananaybatra.reddit I/BrowserStartupController﹕ Initializing chromium process, renderers=0
08-06 21:40:45.919 26681-26681/com.example.ananaybatra.reddit E/AudioManagerAndroid﹕ BLUETOOTH permission is missing!
08-06 21:40:46.068 26681-26681/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-06 21:40:46.083 26681-26681/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-06 21:40:46.083 26681-26681/com.example.ananaybatra.reddit D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-06 21:40:46.544 26681-26703/com.example.ananaybatra.reddit W/chromium﹕ [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
08-06 21:40:46.685 26681-26681/com.example.ananaybatra.reddit D/AndroidRuntime﹕ Shutting down VM
08-06 21:40:46.685 26681-26681/com.example.ananaybatra.reddit W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419a0ba8)
08-06 21:40:46.685 26681-26681/com.example.ananaybatra.reddit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ananaybatra.reddit, PID: 26681
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ananaybatra.reddit/com.example.ananaybatra.reddit.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.webkit.WebView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.webkit.WebView
at com.example.ananaybatra.reddit.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
device not found
这是我的主要活动.java
package com.example.ananaybatra.reddit;
import android.content.Context;
import android.content.Intent;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = this;
mWebView= (WebView) findViewById(R.id.activity_main_webview);
WebSettings websettings = mWebView.getSettings();
websettings.setJavaScriptEnabled(true);
mWebView.loadUrl("https://www.reddit.com/");
mWebView.setWebViewClient(new MyAppWebViewClient());
Intent intent = new Intent(this, WebViewActivity.class);
startActivity(intent);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyAppWebViewClient extends WebViewClient{
}
}
这是我的MyAppWebViewClient,
package com.example.ananaybatra.reddit;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
/**
* Created by ananaybatra on 06/08/15.
*/
public class MyAppWebViewClient extends WebViewActivity {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(Uri.parse(url).getHost().endsWith("html5rock.com")){
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
这是我的Androidmanifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ananaybatra.reddit" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的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" tools:context=".MainActivity"
android:id="@+id/activity_main_webview">
<WebView
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</RelativeLayout>
我启动它后,我的Android应用程序出现意外关机。 一旦我在Android模拟器或手机中启动我的应用程序,就会崩溃说#34;不幸的是,应用已经停止&#34;。
任何帮助将不胜感激。谢谢。 我正在尝试将我的网站转换为Android应用程序。
答案 0 :(得分:2)
您正在将WebView初始化为Relativelayout,请查看以下行
mWebView= (WebView) findViewById(R.id.activity_main_webview);
以下ID activity_main_webview
是RelativeLayout
而不是WebView
的ID。
将该行替换为:
mWebView = (WebView) findViewById(R.id.textView);
P.S尝试为视图使用更具描述性的ID,例如WebView可能具有webview的ID而不是textView :)。
答案 1 :(得分:0)
如果您查看自己的xml,则ip
为activity_main_webview
。并且您将其投放到RelativeLayout
中的WebView
。
您可能想要这样做,
MainActivity.java