我在setAdapter
遇到listView的空指针异常。
listView和Adapter都是 NOT null。
这里的问题是什么?
片段的OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View dataSearch = inflater.inflate(R.layout.fragment_datasearch,container,false);
itemList = (ListView)dataSearch.findViewById(R.id.searchResults);
// Item list is not null
adapter = new SearchAdapter(getActivity(), R.layout.row, item);
// adapter is not null
if (!itemList.equals("")){
itemList.setAdapter(adapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});}
else System.out.println("null");
return dataSearch;
}
&#13;
适配器类
public class SearchAdapter extends ArrayAdapter<FoodItem> {
ArrayList<Item> item;
Context context;
int Resources;
LayoutInflater vi;
public SearchAdapter(Context context, int resource, ArrayList<Item> objects) {
super(context, resource, objects);
item = objects;
Resources = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (convertView == null) {
convertView = vi.inflate(Resources, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.count = (TextView) convertView.findViewById(R.id.count);
holder.brand = (TextView) convertView.findViewById(R.id.brand);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(item.get(position));
holder.name.setText(item.get(position).name);
holder.count.setText(item.get(position).count);
holder.brand.setText(item.get(position).brand);
return convertView;
}
}
class ViewHolder {
public TextView name = null;
public TextView count= null;
public TextView brand=null;
void populateFrom(FoodItem i) {
name.setText(i.name);
count.setText(i.count);
brand.setText(i.brand);
}
}
&#13;
logcat的
java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:486)
at com.smart_mat.dev.fragments.SearchFragment.onCreateView(
SearchFragment.java:86)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
&#13;
答案 0 :(得分:2)
您的item
变量在adapter = new SearchAdapter(getActivity(), R.layout.row, item);
中为空,因此请在之前进行实例化。
答案 1 :(得分:1)
在初始化ListView之前你无法调用setAdapter,尝试放入
itemList=(ListView)dataSearch.findViewById(R.id.itemList);
后
View dataSearch = inflater.inflate(R.layout.fragment_datasearch,container,false);