java – Using a ArrayAdapter won't work with fragment-ThrowExceptions

Exception or error:

I am trying to get an ArrayList from an activity and then display it inside a fragment in a custom adapter. The problem it’s that I cannot setAdapter to do so. The error it says the context it’s wrong, when I created the Adapter.

Creare_istoricAdapter class:

public class Creare_istoricAdapter extends ArrayAdapter<Creare_istoric> {
    private static final String TAG = "Creareistoric_adapter";
    private Context mContext;
    int mResource;

    public Creare_istoricAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Creare_istoric> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String id_alerta = getItem(position).getId_alerta();
        String tip_problema = getItem(position).getTip_problema();
        String data_ora= getItem(position).getData_ora();
        String stare_problema = getItem(position).getStare_problema();
        String descriere = getItem(position).getDescriere();
        Creare_istoric istoric = new Creare_istoric(id_alerta, tip_problema, data_ora, stare_problema, descriere);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);
        TextView text_tip_alerta_istoric = convertView.findViewById(R.id.text_tip_alerta_istoric);
        TextView text_ora_alerta_istoric = convertView.findViewById(R.id.text_ora_alerta_istoric);
        TextView text_stare_alerta_istoric = convertView.findViewById(R.id.text_stare_alerta_istoric);
        text_tip_alerta_istoric.setText(tip_problema);
        text_ora_alerta_istoric.setText(data_ora);
        text_stare_alerta_istoric.setText(stare_problema);
        return convertView;
    }
}

The fragment class:

public class Fragment_alerte_trimise extends Fragment {
    public Fragment_alerte_trimise(){}
    ListView list_view_alerte_trimise;
    ArrayList<Creare_istoric> alerteTrimise= new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View alerte_trimise = inflater.inflate(R.layout.fragment_alerte_trimise,container, false);
        return alerte_trimise;
        Bundle bundle_trimise;
        if (bundle_trimise != null) {
            alerteTrimise = bundle_trimise.getParcelableArrayList("alerte_trimise");
        }
        list_view_alerte_trimise =getView().findViewById(R.id.list_view_alerte_trimise);
        Creare_istoricAdapter adapter = new Creare_istoricAdapter(this, R.layout.istoric_alerte_adapter, alerteTrimise);
        list_view_alerte_trimise.setAdapter(adapter);
    }

}

The problems it’s in new Creare_istoricAdapter(this, R.layout.istoric_alerte_adapter, alerteTrimise) , about the context, and is also greyed out, saying it’s redundant.

EDIT:

I have changed the code following the answers below, but now when I open the activity that uses the fragment, it does not show the list at all. It’s only an blank screen.

The fragment code:

public class Fragment_alerte_trimise extends Fragment {
   public Fragment_alerte_trimise(){}
   private ListView list_view_alerte_trimise;
   ArrayList<Creare_istoric> alerteTrimise= new ArrayList<>();

   @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View alerte_trimise = inflater.inflate(R.layout.fragment_alerte_trimise,container, false);
        return alerte_trimise;
}

   @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle_trimise = getArguments();
        if (bundle_trimise != null) {
            alerteTrimise = bundle_trimise.getParcelableArrayList("alerte_trimise");
        }
        list_view_alerte_trimise =getView().findViewById(R.id.list_view_alerte_trimise);
        list_view_alerte_trimise.setAdapter(new Creare_istoricAdapter(getContext(), R.layout.istoric_alerte_adapter, alerteTrimise));
}

}

The way I created the Parceable:

Bundle bundle_trimise = new Bundle();
bundle_trimise.putParcelableArrayList("alerte_trimise", alerteTrimise);
Fragment_alerte_trimise fg = new Fragment_alerte_trimise();
fg.setArguments(bundle_trimise);

I have the ListView created in the layout that’s inflated by the onCreateView, but when I use getView().findViewById it says that it may produce 'NullPointerException', and so does the getContext(), which says it might be null

How to solve:

You have to use activity context instead of fragment context this. Instead of using getContext() or getActivity() you should initialize the context on onAttach to avoid NullPointerException. Check below:

private Context activityContext;

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    activityContext = context;
}

@Override
public void onDetach() {
    super.onDetach();
    activityContext = null;
}

Beside this, you have to return the view inside onCreateView at the very end. Check below:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    ....

    // use alerte_trimise instead of getView()
    list_view_alerte_trimise = alerte_trimise.findViewById(R.id.list_view_alerte_trimise);


    //use activityContext instead of this
    Creare_istoricAdapter adapter = new Creare_istoricAdapter(activityContext, R.layout.istoric_alerte_adapter, alerteTrimise);

    ....

    return alerte_trimise;
}

In your code, you try to use Bundle after call return. That’s why rest of your code is unreachable.

###

Please try to use getContext() instead of this.

###

You are using Creare_istoricAdapter in your Fragment_alerte_trimise so it needs context. therefor we can give activity context or application context(beyond the activity level)

Moreover…

getContext() – Returns the context view only current running activity.

getActivity()– Return the Activity this fragment is currently associated with.

getActivity() can be used in a Fragment for getting the parent Activity of the Fragment.

Add this code changes to your code.

getActivity().getApplicationContext() 

###

  1. Use getContext() instead of this. this refers to current object (Fragment), which is not subclass of Context.

  2. Create adapter in onViewCreated(), because getContext() returns null in onCreateView().

Leave a Reply

Your email address will not be published. Required fields are marked *