-4

好的,所以我有一个鸡尾酒圣经应用程序,可让您从列表中选择鸡尾酒,它应该显示有关鸡尾酒的各种信息,例如文本、img 和链接 youtube 剪辑的按钮。代码正在编译,但是当我单击其中一个时应用程序意外崩溃的列表中的项目。同样,目前我的应用程序只允许两种鸡尾酒,我如何让它运行 20 种鸡尾酒?顺便说一句,我已经删除了几乎所有的进口商品。

运行列表类的代码

package com.drunktxtapp;

import android.app.Activity;

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) {

                try {
                    Class<?> menuClass = Class.forName("com.drunktxtapp.Menu");
                    Intent ourIntent = new Intent(CocktailMenu.this, menuClass);
                    startActivity(ourIntent);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

        b2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String cocktailType = classes[(int) (Math.random() * classes.length)];
                try{
                Class<?> ourClass = Class.forName("com.drunktxtapp." + cocktailType);
                Intent openRandom = new Intent(CocktailMenu.this, ourClass);
                startActivity(openRandom);
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }
        };
    });
    }
}

生成鸡尾酒列表的代码

package com.drunktxtapp;

import android.app.ListActivity;


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("Bloody_Mary", "Capirinha");
            startActivity(ourIntent);
    }
}

更改鸡尾酒展示页面信息的代码

package com.drunktxtapp;

import android.net.Uri;

public class CocktailDetail extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cocktaildetail);
        ImageView imageView1 = (ImageView)findViewById(R.id.imageCocktail);
        imageView1.setImageDrawable(getResources().getDrawable(R.drawable.bloodymary));
        Button b1 = (Button) findViewById(R.id.bYoutube);
        TextView t1 = (TextView)findViewById(R.id.textCocktailName);
        String cocktailName = getIntent().getStringExtra("Bloody Mary");
        t1.setText(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")));
                }
        });
    }
}

这是 LogCat

04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.os.Looper.loop(Looper.java:137)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.app.ActivityThread.main(ActivityThread.java:4898)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at java.lang.reflect.Method.invoke(Method.java:511)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at dalvik.system.NativeStart.main(Native Method)
04-18 09:22:38.085: E/AndroidRuntime(1068): Caused by: java.lang.NullPointerException
04-18 09:22:38.085: E/AndroidRuntime(1068):     at com.drunktxtapp.CocktailDetail.onCreate(CocktailDetail.java:26)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.app.Activity.performCreate(Activity.java:5206)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
04-18 09:22:38.085: E/AndroidRuntime(1068):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
04-18 09:22:38.085: E/AndroidRuntime(1068):     ... 11 more
04-18 09:27:46.270: I/Process(1068): Sending signal. PID: 1068 SIG: 9

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/beer"
    android:id="@+id/cocktailDetail" >

    <TextView
        android:id="@+id/textCocktailName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Cocktail Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageCocktail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/bloodymary" />

    <TextView
        android:textStyle="bold"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Ingredients"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textIngredients"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Insert txt here"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textStyle="bold" />

    <TextView
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="2dp"
        android:text="Preparation"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textPrepration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Insert txt here"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textStyle="bold" />

    <Button
        android:id="@+id/buttonYoutube"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="YouTube Clip"
        android:textSize="20dp" />

</LinearLayout>
4

3 回答 3

0

你把下划线留在了Bloody_Mary

该声明String cocktailName = getIntent().getStringExtra("Bloody Mary");必须是

String cocktailName = getIntent().getStringExtra("Bloody_Mary");

Button b1 = (Button)findViewById(R.id.buttonYoutube);

于 2013-04-18T08:46:49.003 回答
0

我在您的 xml 文件中没有看到 ID 为“bYoutube”的按钮。看起来当您查找该按钮时,调用返回 null,因此当您尝试设置其 onClickListener 时,您的应用程序崩溃。

于 2013-04-18T08:47:45.460 回答
0

尝试使用:

Button b1 = (Button) findViewById(R.id.buttonYoutube);

因为您的 xml 文件中没有bYoutube按钮。

于 2013-04-18T08:50:03.323 回答