android中的空指针异常

San*_*dey 0 android

我有一个问题,我在button.setOnClickListner上收到了nullpointer异常.我不知道为什么.我建议我解决这个问题.

Error:
05-27 20:22:24.194: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.shopzilla.android.common/org.shopzilla.android.product.ProductShareActivity}: java.lang.NullPointerException
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.os.Looper.loop(Looper.java:123)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at java.lang.reflect.Method.invoke(Method.java:507)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at dalvik.system.NativeStart.main(Native Method)
05-27 20:22:24.194: ERROR/AndroidRuntime(336): Caused by: java.lang.NullPointerException
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at org.shopzilla.android.product.ProductShareActivity.onCreateDialog(ProductShareActivity.java:39)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.Activity.onCreateDialog(Activity.java:2482)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.Activity.createDialog(Activity.java:882)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.Activity.showDialog(Activity.java:2557)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.Activity.showDialog(Activity.java:2524)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at org.shopzilla.android.product.ProductShareActivity.onCreate(ProductShareActivity.java:24)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-27 20:22:24.194: ERROR/AndroidRuntime(336):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
Run Code Online (Sandbox Code Playgroud)

码:

package org.shopzilla.android.product;

import org.shopzilla.android.common.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ProductShareActivity extends Activity{

    Dialog dialog;
    Dialog dialog1;
    Button btn_ok;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_background);
        showDialog(0);
    }

    //Dialog Creation

    protected Dialog onCreateDialog(int id) {

        switch(id) {

        case 0: dialog = new Dialog(ProductShareActivity.this);
            dialog.setContentView(R.layout.sms);
            dialog.setTitle("Please Enter Phone Number");

            final EditText txt_sms = (EditText)findViewById(R.id.txt_sms);
            btn_ok = (Button)dialog.findViewById(R.id.btn_sms_ok);
            btn_ok.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( "sms:" + txt_sms.getText().toString()));
                    intent.putExtra("sms_body", "Title: "+ProductComparisonActivity.s_title+"\n"+"Description: "+ProductComparisonActivity.s_des+"\n"+"\n"+"Max Price: "+ProductComparisonActivity.s_max+"\n"+"Min Price: "+ProductComparisonActivity.s_min); 
                    startActivity(intent);
                }
            });


        default:
            dialog = null;
        }
        return dialog;
    }


}
Run Code Online (Sandbox Code Playgroud)

Kar*_*Øie 5

你必须以'break'结束你的'case 0:',现在它只是继续下去'dialog = null';