java - 不是封闭类错误

标签 java android custom-adapter

我正在尝试设置适配器,但在下面一行的 onCreateView 方法中给我的错误不是封闭类。

CustomAdapter adapter = new CustomAdapter(SecondYearFragment.this, sub);      

这是我的 Actvity.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_main">

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

这是 fragment_third_year.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=".SecondYearFragment">

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

这是 sec_year_dept.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:paddingBottom="@dimen/text_padding_top_bottom"
    android:paddingLeft="@dimen/text_padding_left_right"
    android:paddingRight="@dimen/text_padding_left_right"
    android:paddingTop="@dimen/text_padding_top_bottom"
    android:text="@string/app_name"
    android:textSize="@dimen/text_size"
    android:textStyle="bold" /></RelativeLayout>

这是 ThirdYearFragment 类

public class ThirdYearFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

ListView mListview;
String[] sub = {"random1", "random2", "random3", };

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public ThirdYearFragment() {
    // Required empty public constructor
}

public static ThirdYearFragment newInstance(String param1, String param2) {
    ThirdYearFragment fragment = new ThirdYearFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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


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

    ListView listView = view.findViewById(R.id.listviewsecond);
    CustomAdapter adapter = new CustomAdapter(SecondYearFragment.this, sub);
    listView.setAdapter(adapter);
    return view;
} }

这是我的自定义适配器

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

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

@Override
public int getCount() {
    return subjects.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_year_dept, parent, false);
        mViewHolder.mSubjects = (TextView) convertView.findViewById(R.id.dept_name);
        mViewHolder.mSubjects.setText(subjects[position]);
        convertView.setTag(mViewHolder);
    }else {
        mViewHolder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}
static class ViewHolder {
    TextView mSubjects;
}}

有人可以向我解释发生了什么以及我错过了什么吗?

最佳答案

试试这个:

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

您引用了一个未在当前 fragment SecondYearFragment 中实例化的 fragment 。所以它还没有上下文。

此外, fragment 不是上下文,因此即使它被实例化也不会工作。

关于java - 不是封闭类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50398325/

相关文章:

android - 从自定义适配器中的 ListView 中删除项目

android - OnclickListener 在 fragment ListView 中不起作用

java - java 和 Scala 中非 Ascii 字符的子字符串

java - corba 实现在哪里找到

java - 使用iOS iPad2连接并登录运行HTTP服务的Java服务器

java - 我可以使用什么适配器来填充接受来自 Firestore 的多个查询的 recyclerView?

java - 这个for循环有什么问题?

java - 使用 Quartz 安排在 CronTrigger 触发的作业完成后开始作业

android - 原生 android 和 iOS 应用程序的 GUI 测试

android - 尝试在 customListAdapter 上设置一个 TextView 时,应用程序强制关闭