Android - 应用程序中出现意外崩溃

时间:2013-04-19 04:30:26

标签: android xml class manifest

所以我的问题是我的应用程序中有两个功能。第一个是列表视图,它生成鸡尾酒列表,并允许您从列表中选择一个。这是我的程序意外崩溃的地方。并且随机函数完美地工作。任何人都可以解释为什么我的应用程序没有加载列表视图? CocktailMenu课程

package com.drunktxtapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.app.ListActivity;

public class CocktailMenu extends Activity {

    String classes[] = {"Bloody_Mary", "Capirinha", "Cosmopolitan", "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita", "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada", "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour", "White_Russian"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cocktail_menu);
        Button b1 = (Button) findViewById(R.id.bByList);
        Button b2 = (Button) findViewById(R.id.bRandomCocktail);

        b1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Intent ourIntent = new Intent(CocktailMenu.this, Menu.class);
                ourIntent.putExtra("cocktail_name", classes);
                startActivity(ourIntent);
                }
            }
        );
        b2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                int random = (int) (Math.random() * classes.length);
                Intent i = new Intent(CocktailMenu.this, CocktailDetail.class);
                i.putExtra("cocktail_name", classes[random]);
                startActivity(i);
            }
        }
            );
    }
}

菜单类

package com.drunktxtapp;

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[] = { "Bloody_Mary", "Capirinha", "Cosmopolitan",
            "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita",
            "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada",
            "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour",
            "White_Russian" };

    @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);
        ;
        Intent ourIntent = new Intent(Menu.this, CocktailDetail.class);
        ourIntent.putExtra("cocktail_name",classes);
        startActivity(ourIntent);

    };
}

CocktailDetail类

package com.drunktxtapp;

import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


    public class CocktailDetail extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.cocktaildetail);
            Button b1 = (Button) findViewById(R.id.buttonYoutube);
            String cocktailName = getIntent().getStringExtra("cocktail_name");
            TextView t1 = (TextView)findViewById(R.id.textCocktailName);
            t1.setText(cocktailName);
            ImageView imageView1 = (ImageView)findViewById(R.id.imageCocktail);
            //imageView1.setImageResource(R.drawable.cocktailName);
            b1.setOnClickListener(new Button.OnClickListener() {

                public void onClick(View v) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=Alt-ehDc3fc")));
                    }
            });
        }
    }

CocktailMenu 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/beer"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bByList"
        android:layout_width="250dp"
        android:layout_height="125dp"
        android:layout_gravity="center"
        android:layout_marginBottom="100dp"
        android:layout_marginTop="25dp"
        android:padding="25dp"
        android:text="Cocktail List"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bRandomCocktail"
        android:layout_width="250dp"
        android:layout_height="125dp"
        android:layout_gravity="center"
        android:layout_marginTop="25dp"
        android:padding="2dp"
        android:text="Random Cocktail"
        android:textSize="20dp" />

</LinearLayout>

清单

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

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

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

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CocktailDetail"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.CocktailDetail" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Capirinha"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Capirinha" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Cosmopolitan"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Cosmopolitan" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Cuba_Libre"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Cuba_Libre" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Daiquiri"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Daiquiri" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Mai_Tai"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Mai_Tai" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Manhattan"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Manhattan" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Margarita"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Margarita" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Martini"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Martini" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Mint_Julep"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Mint_Julep" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Mojito"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Mojito" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Old_Fashoned"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Old_Fashoned" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Pina_Colada"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Pina_Colada" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Screwdriver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Screwdriver" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Singapore_Sling"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Singapore_Sling" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Tom_Collins"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Tom_Collins" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Whiskey_Sour"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.Whiskey_Sour" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".White_Russian"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.drunktxtapp.White_Russian" />

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

</manifest>

logcat的

04-19 05:28:41.374: D/dalvikvm(20672): GC_FOR_ALLOC freed 64K, 7% free 12379K/13191K, paused 18ms, total 18ms
04-19 05:28:41.374: I/dalvikvm-heap(20672): Grow heap (frag case) to 12.992MB for 358208-byte allocation
04-19 05:28:41.399: D/dalvikvm(20672): GC_FOR_ALLOC freed 1K, 7% free 12727K/13575K, paused 11ms, total 11ms
04-19 05:28:41.409: V/MediaPlayer-JNI(20672): native_setup
04-19 05:28:41.409: V/MediaPlayer(20672): constructor
04-19 05:28:41.409: V/MediaPlayer(20672): setListener
04-19 05:28:41.409: V/MediaPlayer-JNI(20672): setDataSourceFD: fd 44
04-19 05:28:41.409: V/MediaPlayer(20672): setDataSource(44, 18199, 64493)
04-19 05:28:41.474: V/MediaPlayer(20672): setVideoSurfaceTexture
04-19 05:28:41.474: V/MediaPlayer(20672): prepare
04-19 05:28:41.479: V/MediaPlayer(20672): message received msg=5, ext1=0, ext2=0
04-19 05:28:41.479: V/MediaPlayer(20672): New video size 0 x 0
04-19 05:28:41.479: V/MediaPlayer(20672): callback application
04-19 05:28:41.479: V/MediaPlayer(20672): back from callback
04-19 05:28:41.479: V/MediaPlayer(20672): message received msg=1, ext1=0, ext2=0
04-19 05:28:41.479: V/MediaPlayer(20672): prepared
04-19 05:28:41.479: V/MediaPlayer(20672): signal application thread
04-19 05:28:41.479: V/MediaPlayer(20672): callback application
04-19 05:28:41.479: V/MediaPlayer(20672): prepare complete - status=0
04-19 05:28:41.479: V/MediaPlayer-JNI(20672): start
04-19 05:28:41.479: V/MediaPlayer(20672): start
04-19 05:28:41.479: V/MediaPlayer(20672): back from callback
04-19 05:28:41.549: V/MediaPlayer-JNI(20672): release
04-19 05:28:41.549: V/MediaPlayer(20672): setListener
04-19 05:28:41.549: V/MediaPlayer(20672): disconnect
04-19 05:28:41.554: V/MediaPlayer(20672): destructor
04-19 05:28:41.554: V/MediaPlayer(20672): disconnect
04-19 05:28:41.559: W/MediaPlayer(20672): mediaplayer went away with unhandled events
04-19 05:28:41.559: W/MediaPlayer(20672): mediaplayer went away with unhandled events
04-19 05:28:48.609: D/AndroidRuntime(20672): Shutting down VM
04-19 05:28:48.609: W/dalvikvm(20672): threadid=1: thread exiting with uncaught exception (group=0x40d3c2a0)
04-19 05:28:48.619: E/AndroidRuntime(20672): FATAL EXCEPTION: main
04-19 05:28:48.619: E/AndroidRuntime(20672): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.drunktxtapp/android.view.Menu}; have you declared this activity in your AndroidManifest.xml?
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1420)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Activity.startActivityForResult(Activity.java:3446)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Activity.startActivityForResult(Activity.java:3407)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Activity.startActivity(Activity.java:3617)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.Activity.startActivity(Activity.java:3585)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at com.drunktxtapp.CocktailMenu$1.onClick(CocktailMenu.java:29)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.view.View.performClick(View.java:4211)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.view.View$PerformClick.run(View.java:17267)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.os.Handler.handleCallback(Handler.java:615)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.os.Looper.loop(Looper.java:137)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at java.lang.reflect.Method.invoke(Method.java:511)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-19 05:28:48.619: E/AndroidRuntime(20672):    at dalvik.system.NativeStart.main(Native Method)
04-19 05:28:58.679: I/Process(20672): Sending signal. PID: 20672 SIG: 9

2 个答案:

答案 0 :(得分:4)

The word "Menu" is the error!!!!

android中有一个默认的类菜单。因此建议不要将该名称提供给其他类或活动..

将名为Menu的活动重命名为MyMenu

等其他内容

答案 1 :(得分:3)

嗨您是否在Logcat中看到过这一行?

无法找到显式活动类{com.drunktxtapp / android.view.Menu};你有没有在AndroidManifest.xml中声明这个活动?