简单的WebView崩溃

时间:2014-06-14 12:52:48

标签: android eclipse webview

点击按钮后,应用程序崩溃,不确定原因..

MainActivity:

public class MainActivity extends ActionBarActivity {

private Button button;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Context context = this;

    button = (Button) findViewById(R.id.buttonUrl);

    button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);
            startActivity(intent);
          }

        });

WebViewActivity:

public class WebViewActivity extends MainActivity {

private WebView webView;


public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("http://www.google.com");


}



   }

webview.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:id="@+id/webView1" >   
</WebView>

activity_main.xml中:

<RelativeLayout 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.webview.MainActivity"
tools:ignore="MergeRootFrame" >

<Button
    android:id="@+id/buttonUrl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="124dp"
    android:text="Go to website" />

我正在Android 4.4上测试,并尝试使用相同结果的Nexus 4(崩溃)。

我正在使用本教程:http://www.mkyong.com/android/android-webview-example/

Logcat错误:

06-14 22:01:17.871: D/AndroidRuntime(1049): Shutting down VM

06-14 22:01:17.871:W / dalvikvm(1049):threadid = 1:线程退出未捕获的异常(group = 0xb1a4eba8) 06-14 22:01:17.911:E / AndroidRuntime(1049):致命异常:主要 06-14 22:01:17.911:E / AndroidRuntime(1049):进程:com.example.webview,PID:1049 06-14 22:01:17.911:E / AndroidRuntime(1049):android.content.ActivityNotFoundException:无法找到显式活动类{com.example.webview / com.example.webview.WebViewActivity};你有没有在AndroidManifest.xml中声明这个活动? 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Activity.startActivityForResult(Activity.java:3424) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Activity.startActivityForResult(Activity.java:3385) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Activity.startActivity(Activity.java:3627) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.Activity.startActivity(Activity.java:3595) 06-14 22:01:17.911:E / AndroidRuntime(1049):at com.example.webview.MainActivity $ 1.onClick(MainActivity.java:30) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.view.View.performClick(View.java:4438) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.view.View $ PerformClick.run(View.java:18422) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.os.Handler.handleCallback(Handler.java:733) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.os.Handler.dispatchMessage(Handler.java:95) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.os.Looper.loop(Looper.java:136) 06-14 22:01:17.911:E / AndroidRuntime(1049):在android.app.ActivityThread.main(ActivityThread.java:5017) 06-14 22:01:17.911:E / AndroidRuntime(1049):at java.lang.reflect.Method.invokeNative(Native Method) 06-14 22:01:17.911:E / AndroidRuntime(1049):at java.lang.reflect.Method.invoke(Method.java:515) 06-14 22:01:17.911:E / AndroidRuntime(1049):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 06-14 22:01:17.911:E / AndroidRuntime(1049):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-14 22:01:17.911:E / AndroidRuntime(1049):at dalvik.system.NativeStart.main(Native Method) 06-14 22:01:21.071:I / Process(1049):发送信号。 PID:1049 SIG:9

1 个答案:

答案 0 :(得分:1)

由于您的logcat显示以下错误:

FATAL EXCEPTION: main 06-14 22:01:17.911: E/AndroidRuntime(1049): Process: 
com.example.webview, PID: 1049 06-14 22:01:17.911: E/AndroidRuntime(1049): 
android.content.ActivityNotFoundException: Unable to find explicit activity class 
{com.example.webview/com.example.webview.WebViewActivity}; have you declared this 
activity in your AndroidManifest.xml? 

根据这个例外,我可以说:

1. you have to declare your webviewActivity in your manifest file.
2_. or the declaration might be wrong.that is why when you fire intent to WebViewActivity,
the system coundn't find the activity in manifest file.i.e why it throws activity not found exception.

溶液

<activity
    android:name:"your_package_name.WebViewActivity">
</activity>