提问者:小点点

这在使用自定义适配器的片段中,getContext()可能为空


我已经在一个片段上实现了一个自定义适配器,但它在下面一行的片段的getView方法中产生了错误,

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

下面是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>

这是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>

这是第三个片段.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;

}

下面是CustomAdapter类

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;
}}

我在片段上实现适配器时做错了吗?有人解释一下。


共1个答案

匿名用户

使用view.getContext()。这对我有用