Android - unexpected app crash

2019-09-27 16:17发布

问题:

Ok so I have a cocktail bible app that lets you select a cocktail form a list and it should display various information about the cocktail like text,img and a button to link a youtube clip.The code is compiling but when I click on one of the items in the list the app unexpected crashes. Also at the moment my app only allows two cocktail's how would I get it to run 20 cocktails? I have removed mostly all imports by the way.

Code to run the list class

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();
            }
        };
    });
    }
}

code that produces a list of cocktails

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);
    }
}

Code that changes the information of the cocktail display page

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")));
                }
        });
    }
}

Here is the 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 file

<?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>

回答1:

I don't see the Button with the ID "bYoutube" in your xml file. It looks like when you look for that Button, the call returns null, so when you try to set its onClickListener, your app crashes.



回答2:

Try using:

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

As there's no bYoutube button in your xml file.



回答3:

You left the underscore in Bloody_Mary

The statement String cocktailName = getIntent().getStringExtra("Bloody Mary"); must be

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

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