1

i have implemented a custom adapter on a fragment but it produces the error in the getView method of the fragment on the below line ,

CustomAdapter adapter = new CustomAdapter(this.getContext(), sub);
listView.setAdapter(adapter);

Here is the fragment_third.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThirdFragment">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listviewthird"/></RelativeLayout>

Here is the sec.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/image_below"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:src="@drawable/background_1" />

<TextView
    android:id="@+id/dept_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/app_name"
    android:textSize="@dimen/text_size"
    android:textStyle="bold" /></RelativeLayout>

Here is the ThirdFragment.class

public class ThirdFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";


String[] sub = {"random1", "random2", "random3", };
//code has been vomited.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_third, container, true);

    ListView listView = view.findViewById(R.id.listviewthird);
    CustomAdapter adapter = new CustomAdapter(this.getContext(), sub);
    listView.setAdapter(adapter);
    return view;

}

Here is the CustomAdapter class

public  class CustomAdapter extends ArrayAdapter<String> {
String[] example;
Context mContext;

public CustomAdapter(@NonNull Context context, String[] example) {
    super(context, R.layout.sec);
    this.example = example;
    this.mContext = context;
}

@Override
public int getCount() {
    return example.length;   //returns the size of the list
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder mViewHolder = new ViewHolder();
    if(convertView == null) {
        LayoutInflater mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflator.inflate(R.layout.sec, parent, false);
        mViewHolder.mExample = (TextView) convertView.findViewById(R.id.dept_name);
        mViewHolder.mExample.setText(subjects[position]);
        convertView.setTag(mViewHolder);
    }else {
        mViewHolder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}
static class ViewHolder {
    TextView mExample;
}}

Am i doing wrong while implementing adapter on fragment? Someone explain me.

kk324
  • 11
  • 2
  • Related: [https://stackoverflow.com/questions/47987649/why-getcontext-in-fragment-sometimes-returns-null](https://stackoverflow.com/questions/47987649/why-getcontext-in-fragment-sometimes-returns-null) – Bö macht Blau May 18 '18 at 16:08
  • you implemented correctly :-) if you called api's or background process you will set data when completed. so you may get `NullPointerException` on `getActivity()` and `getContext()` because fragment will detach when your activity is closed that's why warned – Rajasekaran M May 18 '18 at 16:10
  • @RajasekaranM is right, and the best way to get rid of the warning is to do a null check before executing the problematic lines – Bö macht Blau May 18 '18 at 16:14
  • @RajasekaranM I'm getting confused. So should i implement any other method. Is there any example that i can look into. – kk324 May 18 '18 at 16:42
  • @kk324 no need :-) just check `if(getActivity()!=null) {//your adapter creation}` – Rajasekaran M May 18 '18 at 16:44
  • @RajasekaranM Thank you so much :) – kk324 May 18 '18 at 17:13

1 Answers1

1

Use view.getContext(). It works for me

Latif Yaya
  • 171
  • 1
  • 6