borislemke
borislemke

Reputation: 9146

App force closes on button click

I have an Activity with a Button which Intent is relative to how the Activity was launched. The previous Activity is a ListView that passes extra to this Activity to define different values for the Button click Intents. Please take a look at my code below;

public class ContentViewer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getIntent().getExtras();
    int chooser = bundle.getInt("Layout");

    switch(chooser) {
    case 0:
        setContentView(R.layout.about);
        break;

    case 1:
        setContentView(R.layout.contact);
        break;

    case 2:
        setContentView(R.layout.contentviewer);
        break;

    case 3:
        setContentView(R.layout.contact);
        break;

    case 4:
        setContentView(R.layout.contact);
        break;

    case 5:
        setContentView(R.layout.contact);
        break;

    case 6:
        setContentView(R.layout.contact);
        break;

    case 7:
        setContentView(R.layout.contact);
        break;

    case 8:
        setContentView(R.layout.contact);
        break;

    case 9:
        setContentView(R.layout.contact);
        break;
    }

    final int linker = bundle.getInt("Layout");
    Button vlink = (Button)findViewById(R.id.videolink);
    vlink.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View view) {
               switch(linker) {  
            case 0:
                Intent v0 = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("vnd.youtube://" + "0lh_UWF9ZP4"));
                        startActivity(v0); 
                break;
            case 1:
                Intent v1 = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("vnd.youtube://" + "0lh_UWF9ZP4"));
                        startActivity(v1); 
                break;
            case 2:
                Intent v2 = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("vnd.youtube://" + "0lh_UWF9ZP4"));
                        startActivity(v2); 
                break;
            }
        }
    });
}
}

Whenever I click the button, the app force closes. I dont have any idea where my errors might be. Heres the error log;

01-10 00:03:40.895: W/dalvikvm(5759): threadid=1: thread exiting with uncaught exception (group=0x40c271f8)
01-10 00:03:40.900: E/AndroidRuntime(5759): FATAL EXCEPTION: main
01-10 00:03:40.900: E/AndroidRuntime(5759): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.borislemke.cookingwithdog/com.borislemke.cookingwithdog.ContentViewer}: java.lang.NullPointerException
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.os.Looper.loop(Looper.java:137)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread.main(ActivityThread.java:4507)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at dalvik.system.NativeStart.main(Native Method)
01-10 00:03:40.900: E/AndroidRuntime(5759): Caused by: java.lang.NullPointerException
01-10 00:03:40.900: E/AndroidRuntime(5759):     at com.borislemke.cookingwithdog.ContentViewer.onCreate(ContentViewer.java:61)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.Activity.performCreate(Activity.java:4465)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-10 00:03:40.900: E/AndroidRuntime(5759):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-10 00:03:40.900: E/AndroidRuntime(5759):     ... 11 more

Upvotes: 1

Views: 889

Answers (2)

Kyle Clegg
Kyle Clegg

Reputation: 39480

Do you have a button in your XML layout that has the id "videolink"? Your vLink button is null because it can't find the UI element to map to. Can you paste the layout xml?

I'm not familiar with what you're doing with the chooser -- is there not a layout you'd like to load from the beginning that contains the button?

Upvotes: 2

kabuko
kabuko

Reputation: 36312

vlink is null. This is probably because it's not in whichever layout you're loading that causes the crash.

Upvotes: 3

Related Questions