Android自定义适配器-不会返回视图吗?
作者:互联网
我在使用自定义适配器将视图返回到列表视图时遇到问题.这是我在扩展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来工作.我需要创建一个自定义适配器来处理图像数据.
任何帮助将不胜感激.谢谢
解决方法:
我猜您正在尝试查找listPerfilConfigOptions而不是它在哪里:
试试这个代码:
listPerfilConfigOptions =(ListView)view.findViewById(R.id.listEvents);
标签:listview,fragment,custom-adapter,baseadapter,android 来源: https://codeday.me/bug/20191120/2043344.html