문제

Firstly, i apologize but i am a beginner to android development and with the forum. When i go to run the application on my emulator i get the error; "the application (app name) has stopped unexpectedly. Please try again." And i am struggling to interpret my log cat. I have also tried adjusting 'MAIN' and 'MENU' names in the manifest and the src file to match up but know results unless i am missing something. Here is my logcat:

01-06 16:50:32.273: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 652 objects / 51480 bytes in 92ms
01-06 16:50:37.813: D/AndroidRuntime(490): Shutting down VM
01-06 16:50:37.813: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-06 16:50:37.863: E/AndroidRuntime(490): FATAL EXCEPTION: main
01-06 16:50:37.863: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.knight.baseproject/com.knight.baseproject.com.knight.baseproject.MENU}: java.lang.ClassNotFoundException: com.knight.baseproject.com.knight.baseproject.MENU in loader dalvik.system.PathClassLoader[/data/app/com.knight.baseproject-2.apk]
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-06 16:50:37.863: E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 16:50:37.863: E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
01-06 16:50:37.863: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-06 16:50:37.863: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-06 16:50:37.863: E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
01-06 16:50:37.863: E/AndroidRuntime(490): Caused by: java.lang.ClassNotFoundException: com.knight.baseproject.com.knight.baseproject.MENU in loader dalvik.system.PathClassLoader[/data/app/com.knight.baseproject-2.apk]
01-06 16:50:37.863: E/AndroidRuntime(490):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-06 16:50:37.863: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-06 16:50:37.863: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-06 16:50:37.863: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-06 16:50:37.863: E/AndroidRuntime(490):  ... 11 more

Here is the Manifest:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.knight.baseproject.MAIN"
            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.knight.baseproject.MENU"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.knight.baseproject.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is the 'MAIN' java script:

package com.knight.baseproject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MAIN extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread logoTimer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                    Intent menuIntent = new Intent("com.knight.baseproject.MENU");
                    startActivity(menuIntent);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();
                }
            }
        };
        logoTimer.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

The 'MENU' java (which i believe to have no impact):

package com.knight.baseproject;

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

public class MENU extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }



}
도움이 되었습니까?

해결책

<activity
        android:name=".com.knight.baseproject.MENU"

This name starts with a . so it is interpreted as relative to the package name com.knight.baseproject, giving you com.knight.baseproject.com.knight.baseproject.MENU as seen in the exception stacktrace:

java.lang.ClassNotFoundException: com.knight.baseproject.com.knight.baseproject.MENU in loader dalvik.system.PathClassLoader[/data/app/com.knight.baseproject-2.apk]

Either remove the initial . or change the name to just .MENU.

다른 팁

Change

android:name=".com.knight.baseproject.MENU"

to

android:name="com.knight.baseproject.MENU"

in your manifest.

You need to add your activity in your AndroidManifest.xml file.

Try the following options.

Option 1

  1. Right-click on your project
  2. Click Android tools
  3. Click Fix project properties

Option 2

Click Project --> Properties --> Java Build Path --> Order and Export, you should tick the entries you use.

Option 3

Remove all of your libraries except Android.x.x and then cleaning your project by Project --> Clean and then adding your libraries back (don't forget to Check in order and export).

And one more place you need to check is Properties --> Java Compiler --> JDK Compliance.

Try to learn How to debug errors, you can read the stacktrace and also run the application through debugger. Read from top to bottom, you will find what actually happened.

01-06 16:50:37.863: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.knight.baseproject/com.knight.baseproject.com.knight.baseproject.MENU}: java.lang.ClassNotFoundException: com.knight.baseproject.com.knight.baseproject.MENU in loader dalvik.system.PathClassLoader[/data/app/com.knight.baseproject-2.apk]

01-06 16:50:37.863: E/AndroidRuntime(490): Caused by: java.lang.ClassNotFoundException: com.knight.baseproject.com.knight.baseproject.MENU in loader dalvik.system.PathClassLoader[/data/app/com.knight.baseproject-2.apk]

if you read it properly, you would find that an exception occured "ClassNotFoundException" because it couldn't find the class.

I think it's because you didn't add your Activity in your AndroidManifest.xml, whatever activity you use in application should be defined in the manifest file.

It would be great if you could post your code & manifest.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top