我在android中实现了一个简单的webview应用程序(我是android和java的新手)。应用程序工作正常,但当我单击按钮时,我收到此错误。 很遗憾,app已停止错误
错误日志。
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 package 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>
答案 0 :(得分: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>
答案 1 :(得分: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>
答案 2 :(得分:0)
在清单文件中,未声明WebActivity。不要通过在Eclipse中扩展Java类来手动创建Activity,而是执行Project&gt;新的Activity,因此它会自动添加到清单中。