0

I have a problem that, I received nullpointer exception on button.setOnClickListner. I don't know why. PLease suggest me for this problem.

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)

Code:

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


}
1
  • first thing , you have one case , so , why did you use the switch case ?? , second, u should end it with break ; Commented May 27, 2011 at 15:26

4 Answers 4

5

You must end your 'case 0:' with 'break', now it just continues down to 'dialog = null';

Sign up to request clarification or add additional context in comments.

1 Comment

While this is an issue, I am not sure that it is the issue that he is having. His NullPointerException is happening in the onCreateDialog(int) method.
0

About the only things that can be causing the NullPointerException in that method are the findViewById() method. You should make sure you are referencing the proper views for you Dialog.

Also, as @zeropage suggests, you should put the break in your case 0:.

Comments

0

This Error means you uses the value of a control which is not generated. You can check your all code.

Comments

0
dialog.findViewById(R.id.btn_sms_ok)

is returning null . Why , you should find out .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.