This is the java file code for fragment:
@SuppressLint({"NewApi", "ResourceAsColor"})
public void createCardView(String id, String date, final String order) {
flag = true;
cardView = new CardView(context);
cardView.setLayoutParams(layoutParamsCard);
cardView.setElevation(5);
relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(layoutParamsRelative);
textView = new TextView(context);
orderID = new TextView(context);
textView.setLayoutParams(layoutParamsTextView);
textView.setText("Order ID:");
textView.setTextColor(Color.parseColor("#1e1e1e"));
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
orderID.setLayoutParams(layoutParamsTextViewID);
orderID.setText(id);
orderID.setTextColor(Color.parseColor("#646464"));
orderID.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
orderDate = new TextView(context);
orderDate.setText(date);
orderDate.setLayoutParams(layoutParamsOrderDate);
viewOrder = new Button(context);
viewOrder.setText("View Order");
viewOrder.setTextColor(Color.parseColor("#ffffff"));
viewOrder.setBackgroundColor(Color.parseColor("#326432"));
viewOrder.setLayoutParams(layoutParamsViewOrder);
viewOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.view_order_dialog,null);
dialogBuilder.setView(dialogView);
final TextView orderTextView = dialogView.findViewById(R.id.orderTextView);
final Button close = dialogView.findViewById(R.id.close);
orderTextView.setText(order);
dialogBuilder.setTitle("Order ID: " + orderID.getText().toString());
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
});
cardView.addView(relativeLayout);
relativeLayout.addView(textView);
relativeLayout.addView(orderID);
relativeLayout.addView(viewOrder);
relativeLayout.addView(orderDate);
gridLayout.addView(cardView);
}
I am not able to launch custom dialogview from fragment. It crashes the app when i open that fragment. I am using this code to create cards dynamically and creating an OnClickListner for each card.
Your activity context
is null in this line :
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()); // getActivity() is getting null here
To avoid crash, you should wrap your code and check if your activity context is not null like this :
if(getActivity() != null){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.view_order_dialog,null);
dialogBuilder.setView(dialogView);
final TextView orderTextView = dialogView.findViewById(R.id.orderTextView);
orderTextView.setText(order);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
In My Case this type of error is showing in case of displaying Dialog on the RecyclerView item click.
1)Define Activity in your class like this:-
// Initialize activity
Activity activity;
// define activity of this class//
activity=EditFieldsActivity.this;
2)Pass this activity in your Adapter Like this:-
EditFieldAdapter adapter = new EditFieldAdapter(getApplicationContext(),data,activity);
recyclerFieldManagement.setAdapter(adapter);
3)And Get This activity in your adapter and use activity for showing Dialog as:-
private Activity activity;
public EditFieldAdapter(Context applicationContext, List<FieldDetails> data, Activity activity) {
this.ctx=applicationContext;
this.data=data;
this.activity=activity;
}
Hope this will help you.Thanks...
If you use YourActivityName.this
instead of context
, your problem will be solved.
For example ;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Take a Note ");
or
final EditText edtText = new EditText(MainActivity.this);
edtText.setText("editText");
parent.addView(edtText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With