1

我正在使用 The New Boston 教程学习 android 应用程序开发。我完成了第 21 课,但是当我运行我的代码时,应用程序在启动屏幕(即徽标)后崩溃。请帮忙。我不想退出本教程。如果除了下面给出的数据之外,您还需要 eclipse 中的其他任何内容,请告诉我。提前致谢。

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewboston.pok"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Splash"
            android:label="@string/title_activity_the_new_boston" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TheNewBostonActivity"
            android:label="@string/title_activity_the_new_boston" >
            <intent-filter>
                <action android:name="com.thenewboston.pok.THENEWBOSTONACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MENU"
            android:label="@string/title_activity_the_new_boston" >
            <intent-filter>
                <action android:name="com.thenewboston.pok.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的菜单.java:

package com.thenewboston.pok;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{

    String classes[]={"TheNewBostonActivity", "example1", "example2", "example3",
            "example4", "example5", "example6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String cheese = classes[position];
        try{
            Class ourClass = Class.forName("com.thenewboston.pok." + cheese);
            Intent ourIntent = new Intent(Menu.this, ourClass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }

    }
}

我的 Splash.java:

package com.thenewboston.pok;

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

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run() {
                try{
                    sleep(5000);                    
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent open1= new Intent ("com.thenewboston.pok.MENU");
                    startActivity(open1);
                }
            }
        };
        timer.start();
    }

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

}

我的 TheNewBostonActivity.java:

package com.thenewboston.pok;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TheNewBostonActivity extends Activity {
    MediaPlayer ourSong;
    int counter;
    Button add, sub;
    TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ourSong = MediaPlayer.create(TheNewBostonActivity.this, R.raw.undisclosed_desires);
        ourSong.start();
        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.tvDisplay);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your Total Is " + counter);
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Your Total Is " + counter);
            }
        });

    }

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

}

我的日志文件:

09-25 14:56:56.778: D/AndroidRuntime(336): Shutting down VM
09-25 14:56:56.778: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-25 14:56:56.797: E/AndroidRuntime(336): FATAL EXCEPTION: main
09-25 14:56:56.797: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thenewboston.pok/com.thenewboston.pok.MENU}: java.lang.ClassNotFoundException: com.thenewboston.pok.MENU in loader dalvik.system.PathClassLoader[/data/app/com.thenewboston.pok-1.apk]
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-25 14:56:56.797: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
09-25 14:56:56.797: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
09-25 14:56:56.797: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-25 14:56:56.797: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-25 14:56:56.797: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
09-25 14:56:56.797: E/AndroidRuntime(336): Caused by: java.lang.ClassNotFoundException: com.thenewboston.pok.MENU in loader dalvik.system.PathClassLoader[/data/app/com.thenewboston.pok-1.apk]
09-25 14:56:56.797: E/AndroidRuntime(336):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-25 14:56:56.797: E/AndroidRuntime(336):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-25 14:56:56.797: E/AndroidRuntime(336):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-25 14:56:56.797: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
09-25 14:56:56.797: E/AndroidRuntime(336):  ... 11 more
4

1 回答 1

3

你已经用全部大写字母在清单中声明了你的活动。

 android:name=".MENU"

但是您的 Activity 类是 Menu.java

将此更改为

  android:name=".Menu"
于 2012-09-25T16:26:12.937 回答