Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference (Fragment)

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.

like image 898
Owais Yosuf Avatar asked Sep 12 '18 06:09

Owais Yosuf


3 Answers

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();
}
like image 72
Jeel Vankhede Avatar answered Oct 23 '22 10:10

Jeel Vankhede


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...

like image 1
Rahul Kushwaha Avatar answered Oct 23 '22 10:10

Rahul Kushwaha


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);
like image 1
Abdullah alkış Avatar answered Oct 23 '22 11:10

Abdullah alkış