0
這是我爲我的AlertDialog到目前爲止的代碼:如何將自定義列表視圖添加到Android中的AlertDialog?
public class FriendOptionsDialogFragment extends DialogFragment {
public static FriendOptionsDialogFragment newInstance(String firstName) {
FriendOptionsDialogFragment options = new FriendOptionsDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("firstName", firstName);
options.setArguments(bundle);
return options;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_friend_options, null);
Bundle extras = getArguments();
String friendName = extras.getString("firstName");
FriendOptionsAdapter adapter = new FriendOptionsAdapter(getActivity(), friendName);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setView(view);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
我想添加自定義適配器和佈局,以我的AlertDialog的列表視圖。我用的是AlertDialog.Builder對象的方法setAdapter並得到以下錯誤的堆棧跟蹤:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.hb.birthpay.adapter.FriendOptionsAdapter.getView(FriendOptionsAdapter.java:67)
這是錯誤發生在我的自定義適配器行:
holder.friendOptionImageView.setImageResource(mFriendOptionImageId[position]);
我可能做這個錯誤,但是我需要知道如何將自定義列表視圖添加到AlertDialog。
請發佈FriendOptionsAdapter源代碼 – Lino