Java 不是封闭类错误
我正试图设置适配器,但它给我的错误不是下面一行的onCreateView方法中的封闭类Java 不是封闭类错误,java,android,custom-adapter,Java,Android,Custom Adapter,我正试图设置适配器,但它给我的错误不是下面一行的onCreateView方法中的封闭类 CustomAdapter adapter = new CustomAdapter(SecondYearFragment.this, sub); 这是我的activity.xml <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xm
CustomAdapter adapter = new CustomAdapter(SecondYearFragment.this, sub);
这是我的activity.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" />
这是我的自定义适配器
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扩展了ArrayAdapter{
字符串[]主题;
语境;
公共CustomAdapter(@NonNull上下文,字符串[]主题){
super(背景、右布局、部门);
这个。主题=主题;
this.mContext=上下文;
}
@凌驾
public int getCount(){
return subjects.length;//返回列表的大小
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
ViewHolder MVIEHOLDER=新的ViewHolder();
if(convertView==null){
LayoutInflater mInflator=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
convertView=mInflator.充气(R.layout.sec\u year\u dept,parent,false);
mViewHolder.mSubjects=(TextView)convertView.findViewById(R.id.dept_name);
mViewHolder.mSubjects.setText(受试者[位置]);
setTag(mViewHolder);
}否则{
mViewHolder=(ViewHolder)convertView.getTag();
}
返回视图;
}
静态类视窗夹{
text查看mSubjects;
}}
有人能告诉我发生了什么事,我错过了什么吗 试试这个:
CustomAdapter adapter = new CustomAdapter(this.getContext(), sub);
您引用的片段未在当前片段SecondYearFragment
中实例化。所以它还没有上下文
此外,片段不是上下文,因此即使实例化它也无法工作。使用
CustomAdapter=newcustomadapter(getContext(),sub)代码>
您使用的语法用于访问匿名类中的封闭类实例。ThirdYearFragment不是SecondYearFragment的封闭类,这就是为什么会出现此错误 此行的Becz CustomAdapter adapter=新的CustomAdapter(SecondYearFragment.this,sub);使用此而不是SecondYearFragment.this。是的,它正在崩溃应用程序,并说空引用。所以在片段上设置自定义适配器不起作用?
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);