Android中的ClassNotFound异常

时间:2014-06-09 08:22:19

标签: android nullpointerexception android-manifest stack-trace

在使用shouldOverrideUrlLoading方法进行网页加载时,我最终获得了ClassNotFound Exception

StackTrace:

 E/AndroidRuntime(1311): FATAL EXCEPTION: main
 E/AndroidRuntime(1311): Process: com.webpageloader, PID: 1311
 E/AndroidRuntime(1311): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.webpageloader/com.webpageloader.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.webpageloader.MainActivity" on path: DexPathList[[zip file "/data/app/com.webpageloader-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.webpageloader-1, /system/lib]]
 E/AndroidRuntime(1311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
 E/AndroidRuntime(1311):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1311):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1311):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1311):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1311):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1311):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1311):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1311):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1311):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1311): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.webpageloader.MainActivity" on path: DexPathList[[zip file "/data/app/com.webpageloader-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.webpageloader-1, /system/lib]]
 E/AndroidRuntime(1311):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
 E/AndroidRuntime(1311):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
 E/AndroidRuntime(1311):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
 E/AndroidRuntime(1311):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
 E/AndroidRuntime(1311):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
 E/AndroidRuntime(1311):    ... 11 more

WebPageLoader.java:

package com.webpageloader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebPageLoader extends Activity
{
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("http://developer.android.com");
    }
}       

activity_main.xml中:

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

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.webpageloader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

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

我不知道如何解决这些问题。任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:2)

更改

<activity
            android:name="com.webpageloader.MainActivity">

<activity
            android:name="com.webpageloader.WebPageLoader">

错误说Caused by: java.lang.ClassNotFoundException: Didn't find class "com.webpageloader.MainActivity"。 (JVM)尝试加载特定的类,并且在类路径中找不到指定的类。这意味着没有名为MainActivity的班级。

AndroidManifest.xml他已定义<activity android:name="com.webpageloader.MainActivity">,因此它正在尝试查找班级MainActivity,但他已将该班级重命名为WebPageLoader并忘记更改{ {1}}。

答案 1 :(得分:1)

通过更改清单中的ClassNotFoundException来思考并解决这些Activity name

更改这些

 <activity
            android:name="com.webpageloader.MainActivity" > 

进入

<activity
            android:name="com.webpageloader.WebPageLoader" > 

ClassNotFound会发生异常,因为如果我们忘记在Manifest中定义Exact Class Name。我们在运行时看到的异常。