自定义警报对话框中的OnClickListener问题

时间:2013-06-25 10:18:23

标签: android android-preferences android-alertdialog

这是我的OnpreferenceListener。我在Alert Dialog中使用自定义布局,并尝试在自定义布局中查找单击。在OnClickListener方法上获取Null指针异常。任何人都可以指出这里出了什么问题吗?

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

2 个答案:

答案 0 :(得分:1)

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

应该是

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

假设您已在XML布局中将android:id="@+id/rate_now"声明为ImageView

答案 1 :(得分:0)

尝试Carnal's回答您的错误

以及return false

之前的结尾

更改

myAlertDialog.show();

myAlertDialog.create().show();