0

这是我的OnpreferenceListener。我在警报对话框中使用自定义布局并尝试在自定义布局中找到单击。在方法上获取空指针异常OnClickListener。谁能指出这里出了什么问题?

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

2 回答 2

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

应该

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

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

于 2013-06-25T10:23:30.087 回答
0

尝试Carnal's回答您的错误

并且在最后之前return false

改变

myAlertDialog.show();

myAlertDialog.create().show();
于 2013-06-25T10:28:25.350 回答