我希望在单击RecyclerView时打开DialogBox,并且此RecyclerView使用自定义适配器。我在适配器中编写了开放的DialogBox代码,但在运行应用程序时遇到了错误。
我的适配器代码:
public class CardViewDataAdapter_smiley extends RecyclerView.Adapter<CardViewDataAdapter_smiley.ViewHolder> {
public String[] mDataset;
static public Context context;
// Provide a suitable constructor (depends on the kind of dataset)
public CardViewDataAdapter_smiley(String[] myDataset) {
this.mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public CardViewDataAdapter_smiley.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_smiley, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.tvtinfo_text.setText(mDataset[position].toString());
viewHolder.versionName = mDataset[position];
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvtinfo_text;
public String versionName;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvtinfo_text = (TextView) itemLayoutView
.findViewById(R.id.cardview_title_smiley);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_show__sms__dialog);
Button close_btn = (Button) dialog.findViewById(R.id.dialog_close_button);
close_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "test click: " + versionName,
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
适配器中的onClickListener代码:
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_show__sms__dialog);
Button close_btn = (Button) dialog.findViewById(R.id.dialog_close_button);
close_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
错误:
03-09 16:59:53.066 1215-1215/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tellfa.smsbox, PID: 1215
java.lang.NullPointerException
at android.app.Dialog.<init>(Dialog.java:156)
at android.app.Dialog.<init>(Dialog.java:133)
at com.tellfa.smsbox.CardViewDataAdapter_smiley$ViewHolder$1.onClick(CardViewDataAdapter_smiley.java:73)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
@Override
public CardViewDataAdapter_smiley.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
context = parent.getContext();
View itemLayoutView = LayoutInflater.from(context).inflate(
R.layout.cardview_smiley, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}