我在使用自定义适配器将视图返回到listview时遇到问题。这就是我在扩展Fragment的主类中所拥有的。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perfil_config, container, false);
ListAdapter adapter = new PerfilOptionsAdapter(listOptions(), getActivity());
listPerfilConfigOptions = (ListView) getActivity().findViewById(R.id.listEvents);
listPerfilConfigOptions.setAdapter(adapter);
return view;
}
这就是我对名为PerfilOptionsAdapter的自定义适配器所拥有的内容:
public class PerfilOptionsAdapter extends BaseAdapter {
private Activity activity;
private List<PerfilConfigOption> options;
private DateHelper dateHelper = new DateHelper();
public PerfilOptionsAdapter(List<PerfilConfigOption> options, Activity activity) {
this.options = options;
this.activity = activity;
}
@Override
public int getCount() {
return options.size();
}
@Override
public Object getItem(int position) {
return options.get(position);
}
@Override
public long getItemId(int position) {
PerfilConfigOption option = options.get(position);
return option.getOptionID();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View line = inflater.inflate(R.layout.perfilconfigoptions_list , null);
PerfilConfigOption option = options.get(position);
TextView optionName = (TextView) line.findViewById(R.id.perfilConfigOptionName);
optionName.setText(option.getOptionName());
//ImageView photo = (ImageView) line.findViewById(R.id.perfilConfigOptionImage);
//photo.setImageBitmap(option.getOptionImage());
return line;
}
}
Logcat响应:
05-07 19:50:07.677 1730-1730/com.cupdev.voucomo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cupdev.voucomo, PID: 1730
java.lang.NullPointerException
at com.cupdev.voucomo.Fragments.PerfilConfigFragment.onCreateView(PerfilConfigFragment.java:37)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我不确定为什么这个适配器根本不会返回任何视图?我已经尝试使用具有相同数据的SimpleAdapter并且它可以工作。我需要创建一个自定义适配器来处理图像数据。
非常感谢任何帮助。感谢
答案 0 :(得分:3)
我猜你试图找到listPerfilConfigOptions而不是它:
试试这段代码:
listPerfilConfigOptions = (ListView) view.findViewById(R.id.listEvents);
答案 1 :(得分:0)
谢谢大家的帮助,这个bug很简单,而不是放置R.id.listPerfilConfigOptions我放了R.id.listEvents。