片段中的列表视图setAdapter,空指针异常
List view setAdapter in fragment, null pointer exception
我 运行 在 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;
}
适配器class
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);
}
}
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.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)
你不能在初始化你的ListView之前调用setAdapter,尝试把
itemList=(ListView)dataSearch.findViewById(R.id.itemList);
之后
View dataSearch = inflater.inflate(R.layout.fragment_datasearch,container,false);
您的 item
变量在 adapter = new SearchAdapter(getActivity(), R.layout.row, item);
中为 null,因此请先实例化它。
我 运行 在 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;
}
适配器class
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);
}
}
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.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)
你不能在初始化你的ListView之前调用setAdapter,尝试把
itemList=(ListView)dataSearch.findViewById(R.id.itemList);
之后
View dataSearch = inflater.inflate(R.layout.fragment_datasearch,container,false);
您的 item
变量在 adapter = new SearchAdapter(getActivity(), R.layout.row, item);
中为 null,因此请先实例化它。