简体   繁体   中英

android: unfortunately app has stopped error

i was implementing a simple webview app in android (I'm new to android and java). App works fine but when i click the button i get this error. unfortunately app has stopped error

error log.

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)

i am following this tutorial.

here is my code:

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>

You need to make an entry for WebActivity in the manifest file.

If you check the stack trace it says

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

Make an entry as below

       <activity 
        android:name="com.example.androidwebviewexample.WebActivity">
       </activity>

Try this in your manifest..

<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>

In your manifest file, WebActivity is not declared. Do not create the Activity manually by extending Java class in Eclipse, but do Project > new Activity, so it'll automatically get added to the manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM