Android 自定义适配器 - 不会 return 查看?
Android Custom Adapter - Won't return view?
我在使用自定义适配器return将我的视图转换为列表视图时遇到问题。这就是我在扩展 Fragment.
的主要 class 中所拥有的
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.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)
我不确定为什么这个适配器根本 return 没有任何视图?我已经使用具有相同数据的 SimpleAdapter 对其进行了尝试,并且可以正常工作。我需要创建一个自定义适配器来处理图像数据。
如有任何帮助,我们将不胜感激。谢谢
我猜你想找到 listPerfilConfigOptions 而不是它所在的位置:
试试这个代码:
listPerfilConfigOptions = (ListView) view.findViewById(R.id.listEvents);
谢谢大家帮助我,错误很简单,而不是放置 R.id.listPerfilConfigOptions 我放置 R.id.listEvents.
我在使用自定义适配器return将我的视图转换为列表视图时遇到问题。这就是我在扩展 Fragment.
的主要 class 中所拥有的 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.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)
我不确定为什么这个适配器根本 return 没有任何视图?我已经使用具有相同数据的 SimpleAdapter 对其进行了尝试,并且可以正常工作。我需要创建一个自定义适配器来处理图像数据。
如有任何帮助,我们将不胜感激。谢谢
我猜你想找到 listPerfilConfigOptions 而不是它所在的位置:
试试这个代码:
listPerfilConfigOptions = (ListView) view.findViewById(R.id.listEvents);
谢谢大家帮助我,错误很简单,而不是放置 R.id.listPerfilConfigOptions 我放置 R.id.listEvents.