intrepidkarthi
intrepidkarthi

Reputation: 3102

Issue with the OnClickListener inside Custom Alert Dialog

Here is my OnpreferenceListener. I am using a custom layout inside Alert Dialog and trying to find a click inside the custom layout. Getting Null pointer exception on the OnClickListener method. Can anyone point out what is going wrong here?

sendus_feedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        ContextThemeWrapper wrapper = new ContextThemeWrapper(SettingsActivity.this,
                                                              android.R.style.Theme_Holo_Light);
        @SuppressWarnings("unused")
        final LayoutInflater inflater = (LayoutInflater) wrapper
            .getSystemService(LAYOUT_INFLATER_SERVICE);

        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(wrapper);
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View feedbackView = layoutInflater.inflate(R.layout.rate_now_layout, null);

        myAlertDialog.setView(feedbackView);

        ImageView ratenow = (ImageView) feedbackView
            .findViewById(R.drawable.rate_now);
        ratenow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("clicked ratenow", "yes");
                 Uri uri = Uri.parse("market://details?id="
                                    + getApplicationContext()
                                    .getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getApplicationContext(),
                                   "Couldn't launch the market",
                                   Toast.LENGTH_LONG).show();
                }
            }
        });
        myAlertDialog.setPositiveButton("No, thanks.",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface arg0,
                                                                int arg1) {
                                            }
                                        });
        myAlertDialog.setNegativeButton("Ask me later",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface arg0,
                                                                int arg1) {
                                            }
                                        });
        myAlertDialog.show();
        return false;
    }
});

Upvotes: 0

Views: 164

Answers (2)

Michael Shrestha
Michael Shrestha

Reputation: 2555

try Carnal's answer for your error

and also at the end before return false

change

myAlertDialog.show();

to

myAlertDialog.create().show();

Upvotes: 0

Carnal
Carnal

Reputation: 22064

ImageView ratenow = (ImageView) feedbackView.findViewById(R.drawable.rate_now);

should be

ImageView ratenow = (ImageView) feedbackView.findViewById(R.id.rate_now);

Assuming you have declared android:id="@+id/rate_now" in your XML layout as the ImageView

Upvotes: 1

Related Questions