Hung Wei Chun
Hung Wei Chun

Reputation: 145

Alertdialog with custom layout button

I have a layout for my alertdialog.And there is a button to call timepicker. But I don't know how to write the button's setonClickListener. It always crashed.

FATAL EXCEPTION: main

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at prototype.feedme.cat.prototype.activity.CatActivity$3.onClick(CatActivity.java:115)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Code:

private Button time;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.CatActivity);
    time=(Button)findViewById(R.id.time);
    time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TimePickerDialog tpd=new TimePickerDialog(CatActivity.this, mTimesetlistener, mHour, mMinute, true);
            tpd.show();
        }
    });
    clk_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog alertDialog=new AlertDialog.Builder(CatActivity.this)
                    .setTitle("Hi")
                    .setMessage("Are you ok?")
                    .setView(R.layout.notificaton_setting)
                    .show();
        }
    });
}

Upvotes: 0

Views: 1490

Answers (2)

Leonidos
Leonidos

Reputation: 10518

You said that the button is inside dialog. But you try to find it in activity. I mean that until you try to show your dialog there it no the button at all and you try to find it from activity onCreate method.

What you need to do is to find your button in Dialog view. You can do it this way:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.notificaton_setting, null);
dialogLayout.findViewById(R.id.time).setOnClickListener(...)

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

Upvotes: 1

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

Initialize your clk_btn before you call setOnClickListener

private Button time;
private Button clk_btn; //<-- ADD THIS
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
time=(Button)findViewById(R.id.time);
time.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TimePickerDialog tpd=new TimePickerDialog(CatActivity.this, mTimesetlistener, mHour, mMinute, true);
        tpd.show();
    }
});
clk_btn = (Button) findViewById(R.id.YOUR_ID) //<-- AND ADD THIS
clk_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this)
                .setTitle("Hi")
                .setMessage("Are you ok?")
                .setView(R.layout.notificaton_setting)
                .show();
    }
});

}

Upvotes: 0

Related Questions