0

我在 android 中实现了一个简单的 webview 应用程序(我是 android 和 java 的新手)。应用程序工作正常,但是当我单击按钮时出现此错误。 不幸的是,应用程序已停止错误

错误日志。

08-26 11:49:06.374: W/dalvikvm(754): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-26 11:49:06.444: E/AndroidRuntime(754): FATAL EXCEPTION: main
08-26 11:49:06.444: E/AndroidRuntime(754): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.androidwebviewexample/com.example.androidwebviewexample.WebActivity}; have you declared this activity in your AndroidManifest.xml?
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Activity.startActivityForResult(Activity.java:3390)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Activity.startActivityForResult(Activity.java:3351)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Activity.startActivity(Activity.java:3587)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.Activity.startActivity(Activity.java:3555)
08-26 11:49:06.444: E/AndroidRuntime(754):  at com.example.androidwebviewexample.MainActivity$1.onClick(MainActivity.java:28)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.view.View.performClick(View.java:4240)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.view.View$PerformClick.run(View.java:17721)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.os.Handler.handleCallback(Handler.java:730)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.os.Looper.loop(Looper.java:137)
08-26 11:49:06.444: E/AndroidRuntime(754):  at android.app.ActivityThread.main(ActivityThread.java:5103)
08-26 11:49:06.444: E/AndroidRuntime(754):  at java.lang.reflect.Method.invokeNative(Native Method)
08-26 11:49:06.444: E/AndroidRuntime(754):  at java.lang.reflect.Method.invoke(Method.java:525)
08-26 11:49:06.444: E/AndroidRuntime(754):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-26 11:49:06.444: E/AndroidRuntime(754):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-26 11:49:06.444: E/AndroidRuntime(754):  at dalvik.system.NativeStart.main(Native Method)

我正在关注本教程。

这是我的代码:

MainActivity.java

package com.example.androidwebviewexample;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

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

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

        button.setOnClickListener(new OnClickListener() {

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

WebActivity.java 包 com.example.androidwebviewexample;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {

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

        webView = (WebView) findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.loadUrl("http://www.random.com/");
    }

}

androidmanifesto.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidwebviewexample"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8"  android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET" />    

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidwebviewexample.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>
4

3 回答 3

1

您需要WebActivity在清单文件中创建一个条目。

如果您检查堆栈跟踪,它会说

have you declared this activity in your AndroidManifest.xml? 
// you have declared the activity in manifest file

输入如下

       <activity 
        android:name="com.example.androidwebviewexample.WebActivity">
       </activity>
于 2013-08-26T16:15:03.127 回答
1

在你的清单中试试这个..

<activity
            android:name="com.example.androidwebviewexample.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>

<activity 
        android:name="com.example.androidwebviewexample.WebActivity">
    </activity>
于 2013-08-26T16:17:23.777 回答
0

在您的清单文件中,未声明 WebActivity。不要通过在 Eclipse 中扩展 Java 类来手动创建 Activity,而是执行 Project > new Activity,因此它会自动添加到清单中。

于 2013-08-26T16:14:50.083 回答