单击按钮时应用程序强制关闭

时间:2012-01-09 23:10:52

标签: android android-activity forceclose

我有Activity Button IntentActivity的启动方式有关。之前的Activity是一个ListView,可以向Activity传递额外内容,为Button点击Intents定义不同的值。请看下面的代码;

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

每当我点击按钮时,app力就会关闭。我不知道我的错误可能在哪里。继承错误日志;

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

2 个答案:

答案 0 :(得分:3)

vlink为空。这可能是因为它不是你加载的导致崩溃的布局。

答案 1 :(得分:2)

您的XML布局中是否有一个ID为“videolink”的按钮?您的vLink按钮为null,因为它无法找到要映射到的UI元素。你可以粘贴布局xml吗?

我不熟悉你在选择器上做了什么 - 是不是你想从包含按钮的开头加载一个布局?