我是Android的新手,我正在尝试创建一个带有文本和图片的列表视图,并将其放入片段中。我为列表创建了适配器,但是当我尝试设置此适配器时,我会收到以下错误。如果有人可以帮助我找到解决这个问题的解决方案,我会很高兴。
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
这是我的此片段的代码
package com.example.gymshark;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.gymprogress.R;
public class NotificationFragment extends Fragment {
ListView listView;
String mTitle[] = {"Drink water", "Creatine"};
String mDescription[] = {"Water", "Creatine"};
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView)view.findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(getActivity(), mTitle, mDescription);
listView.setAdapter(adapter);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.row_notification, container, false);
}
class MyAdapter extends ArrayAdapter<String>
{
Context context;
String rTitle[];
String rDescription[];
MyAdapter (Context c, String[] title, String[] description)
{
super(c, R.layout.row_notification, R.id.text1, title);
this.context = c;
this.rTitle = title;
this.rDescription = description;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row_notification, parent, false);
TextView myTitle = row.findViewById(R.id.text1);
TextView myDescription = row.findViewById(R.id.text2);
myTitle.setText(rTitle[position]);
myDescription.setText(rDescription[position]);
return row;
}
}
}
首先,请检查您的xml是否有" ListView" listView。
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MyAdapter adapter = new MyAdapter(getActivity(), mTitle, mDescription);
listView.setAdapter(adapter);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.row_notification, container, false);
this.listView = (ListView)view.findViewById(R.id.listView);
return view;
}
在OnViewCreated中复制您的代码
而不是
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView)view.findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(getActivity(), mTitle, mDescription);
listView.setAdapter(adapter);
}
使用此
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
listView = (ListView)view.findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(getActivity(), mTitle, mDescription);
listView.setAdapter(adapter);
}
的问题
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.row_notification, container, false);
}
您使用了错误的布局文件,因此请使用片段布局文件,而不是row_notification