0

嘿,我是 android 编码的初学者,我一直在 youtube 上关注这个系列。每当我将我的项目作为 android 应用程序运行并转到模拟器以选择它时,它都会给我“抱歉 - 应用程序 (Appname) 已意外停止。请重试” Eclipse 不会给我任何错误行来指出问题所在和所以这非常令人沮丧。我一直在互联网上寻找答案,但我无法解决它。我认为这与我的清单有关。每当我运行我的应用程序时,我的 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

4 回答 4

1

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

代替

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

  <activity
                android:name="com.example.thepasics.menu"
                android:label="@string/app_name" >
于 2013-07-20T17:47:35.760 回答
1

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

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

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

此外,您的课程名为menu,不Menu记得它是区分大小写的。Java 约定具有以大写字母开头的类名,因此应该将其更正为Menu.

于 2013-07-20T17:47:42.127 回答
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".

于 2013-07-21T12:30:01.067 回答
0

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

public class menu extends Activity{

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

于 2013-07-20T17:46:32.970 回答