Eclipse“抱歉 - 应用程序(Appname)意外停止。请再试一次”

时间:2013-07-20 17:41:01

标签: java android xml eclipse manifest

嘿,我是Android编码的初学者,我一直在youtube上关注这个系列。每当我将我的项目作为Android应用程序运行并转到模拟器选择它时,它就会给我“抱歉 - 应用程序(Appname)意外停止。请再试一次”Eclipse没有给我任何错误信息,问题出在哪里所以这是非常令人沮丧的..我一直在互联网上寻找答案,但我还没有能够解决它。我认为这与我的Manifest有关。每当我运行我的应用程序时,我的LogCat都会出错。

我的应用程序是一个小应用程序,选择后将首先打开徽标屏幕3秒钟,然后它将带您进入一个界面,有2个按钮可供选择。这两个按钮都会导致编辑文本。

我已经在下面发布了我的所有代码,请大家记住,当你回答我是一个初学者并且可能不理解某些术语时。谢谢!

当我尝试启动时,这是我的LogCat

07-20 20:19:17.382: D/dalvikvm(254): GC_EXTERNAL_ALLOC freed 663 objects / 51912 bytes in 177ms
07-20 20:19:19.791: D/AndroidRuntime(254): Shutting down VM
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.os.Looper.loop(Looper.java:123)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-20 20:19:19.811: E/AndroidRuntime(254):  at java.lang.reflect.Method.invokeNative(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254):  at java.lang.reflect.Method.invoke(Method.java:521)
07-20 20:19:19.811: E/AndroidRuntime(254):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 20:19:19.811: E/AndroidRuntime(254):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 20:19:19.811: E/AndroidRuntime(254):  at dalvik.system.NativeStart.main(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): Caused by: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-20 20:19:19.811: E/AndroidRuntime(254):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-20 20:19:19.811: E/AndroidRuntime(254):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-20 20:19:19.811: E/AndroidRuntime(254):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-20 20:19:19.811: E/AndroidRuntime(254):  ... 11 more

我的清单

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


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.thepasics.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="Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.thePasics.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         <activity
            android:name=".TutorialOne"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.thePasics.TUTORIALONE" />

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

</manifest>

我有3个Java文件,它们是Menu.java Main.java TutorialOne.java。这是主要的一个

package com.example.thepasics;

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


public class Main extends Activity {
    MediaPlayer logoMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        logoMusic = MediaPlayer.create(Main.this, R.raw.music);
        logoMusic.start();

        Thread logoTimer = new Thread(){
            public void run(){
                try{
                    sleep(2000);
                    Intent menuIntent = new Intent("com.example.thePasics.MENU");
                    startActivity(menuIntent);

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

                finally{
                    finish();
                }
            }

        };
        logoTimer.start();

    }

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

}

这是一个menu.java

package com.example.thepasics;

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

public class menu extends Activity{

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

        //Button sound
        final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.buttonsound);

        //Setting up button references
        Button tut1 = (Button) findViewById(R.id.button1); 
        Button tut2 = (Button) findViewById(R.id.button2); 

        tut1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.thepasics.TUTORIALONE"));

            }
        });

        tut2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.thepasics.TutorialOne"));

            }
        });
    }

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


}

第三个是TutorialOne.java

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TutorialOne extends Activity implements OnCheckedChangeListener{

    TextView textOut;
    EditText textIn;
    RadioGroup gravityG, styleG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial1);
        textOut = (TextView) findViewById(R.id.tvChange);
        textIn = (EditText) findViewById(R.id.editText1);
        gravityG = (RadioGroup) findViewById(R.id.rgGravity);
        gravityG.setOnCheckedChangeListener(this);
        styleG = (RadioGroup) findViewById(R.id.rgStyle);
        styleG.setOnCheckedChangeListener(this);
        Button gen = (Button) findViewById(R.id.bGenerate);

        gen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                textOut.setText(textIn.getText());

            }
        });

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch(checkedId){
        case R.id.rbLeft: 
            textOut.setGravity(Gravity.LEFT);
            break;
        case R.id.rbCenter: 
            textOut.setGravity(Gravity.CENTER);
            break;
        case R.id.rbRight: 
            textOut.setGravity(Gravity.RIGHT);
            break;  

        case R .id.rbNormal:
            textOut.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
            break;
        case R .id.rbItalic:
            textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
            break;
        case R .id.rbBold:
            textOut.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD), Typeface.BOLD);
            break;

        }



    }

}

我的3个xml文件是splash,tutorial1和main

这个是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundwithoutericapp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Choose a function" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textSize="25dp"
        android:textStyle="bold" 
        android:id="@+id/button1"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:textSize="25dp"
        android:textStyle="bold" 
        android:id="@+id/button2"/>
</LinearLayout>

第二个是splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/background">


</LinearLayout>

第三个是tutorial1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/tutorialonebackground"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <TextView
            android:id="@+id/tvStyle"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Style"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvGravity"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Gravity"
            android:textSize="25dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <RadioGroup
            android:id="@+id/rgStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/rbNormal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Normal" />

            <RadioButton
                android:id="@+id/rbItalic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Italic" />

            <RadioButton
                android:id="@+id/rbBold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bold" />
        </RadioGroup>

        <RadioGroup
            android:id="@+id/rgGravity"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/rbLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Left" />

            <RadioButton
                android:id="@+id/rbCenter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Center" />

            <RadioButton
                android:id="@+id/rbRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:id="@+id/tvChange"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Type in Text and Press the Button Below" />

    <Button
        android:id="@+id/bGenerate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Generate" />

</LinearLayout>

4 个答案:

答案 0 :(得分:1)

您在manifest.xml中指定了错误的类名

替换

<activity
            android:name="Menu"
            android:label="@string/app_name" >

  <activity
                android:name="com.example.thepasics.menu"
                android:label="@string/app_name" >

答案 1 :(得分:1)

您的清单和菜单活动不匹配。你的清单有

<activity
    android:name="Menu" ...

首先不是有效名称。它可以android:name=".Menu"引用com.com.example.thepasics.Menu,也可以使用完全限定名称(就像您对com.example.thepasics.Main所做的那样)。

此外,您的班级名为menu,而不是Menu - 请记住它区分大小写。 Java约定的类名以大写字母开头,因此它应该更正为Menu

答案 2 :(得分:0)

您的菜单类实际上称为“菜单”,而不是“菜单”。

public class menu extends Activity{

尝试在整个应用中将"com.example.thePasics.MENU"更改为"com.example.thePasics.menu"

答案 3 :(得分:0)

我只看了你的Logcat:

07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]

您的应用程序崩溃了,因为找不到com.example.thepasics.Menu,并且还使用android:name=".Menu"代替android:name="Menu"