无法使用 Android 中的 BaseAdapter 填充自定义 listView
Unable to populate custom listView using BaseAdapter in Android
我是 Android 的新手。我正在使用 BaseAdapter 创建自定义 listView。
我尝试了这段代码,但我的应用程序崩溃了。
我不知道我哪里弄错了?
AreaFragment
public class AreaFragment extends Fragment{
private ListView list;
private Context c;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_area, container, false);
list = (ListView)rootView.findViewById(R.id.areaList);
list.setAdapter(new myAdapter(c));
class Row{
String Names;
String areaNames;
Row(String areaNames,String areaLocality) {
this.Names=areaNames;
this.areaNames=areaLocality;
}
}
class myAdapter extends BaseAdapter{
ArrayList<Row> list;
Context context;
public myAdapter(Context c) {
context =c;
list = new ArrayList<Row>();
Resources res = c.getResources();
String[] names = res.getStringArray(R.array.names);
String[] areasName = res.getStringArray(R.array.areasName);
for(int i=0;i<10;i++){
list.add(new Row(names[i],areasName[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row, parent, false);
TextView areaNames = (TextView) row.findViewById(R.id.mainTextView);
TextView areaLocality = (TextView) row.findViewById(R.id.subTextView);
Row temp = list.get(position);
areaNames.setText(temp.Names);
areaLocality.setText(temp.areaNames);
return row;
}
}
}
Strings.xml
<string-array name="names">
<item >Derek</item>
<item >David</item>
<item >ABC</item>
</string-array>
<string-array name="areasName">
<item >India</item>
<item >UAE</item>
<item >XYZ</item>
</string-array>
Row.xml
在这个文件中我没有什么了不起的。它只包含两个文本视图。
日志猫
FATAL EXCEPTION: main
Process: com.threepin.deepakcorporation, PID: 14033
java.lang.NullPointerException
at com.threepin.deepakcorporation.AreaFragment$myAdapter.<init> (AreaFragment.java:73)
at com.threepin.deepakcorporation.AreaFragment.onCreateView(AreaFragment.java:38)
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:5127)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
首先在 onCreateView()
改变这个
list.setAdapter(new myAdapter(c));
到
list.setAdapter(new myAdapter(this.getActivity()));
因为 'c' 将为空。
接下来,
如上。
改变
for(int i=0;i<10;i++)
到
for(int i=0;i<oneofarray.length;i++)
我是 Android 的新手。我正在使用 BaseAdapter 创建自定义 listView。 我尝试了这段代码,但我的应用程序崩溃了。 我不知道我哪里弄错了?
AreaFragment
public class AreaFragment extends Fragment{
private ListView list;
private Context c;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_area, container, false);
list = (ListView)rootView.findViewById(R.id.areaList);
list.setAdapter(new myAdapter(c));
class Row{
String Names;
String areaNames;
Row(String areaNames,String areaLocality) {
this.Names=areaNames;
this.areaNames=areaLocality;
}
}
class myAdapter extends BaseAdapter{
ArrayList<Row> list;
Context context;
public myAdapter(Context c) {
context =c;
list = new ArrayList<Row>();
Resources res = c.getResources();
String[] names = res.getStringArray(R.array.names);
String[] areasName = res.getStringArray(R.array.areasName);
for(int i=0;i<10;i++){
list.add(new Row(names[i],areasName[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row, parent, false);
TextView areaNames = (TextView) row.findViewById(R.id.mainTextView);
TextView areaLocality = (TextView) row.findViewById(R.id.subTextView);
Row temp = list.get(position);
areaNames.setText(temp.Names);
areaLocality.setText(temp.areaNames);
return row;
}
}
}
Strings.xml
<string-array name="names">
<item >Derek</item>
<item >David</item>
<item >ABC</item>
</string-array>
<string-array name="areasName">
<item >India</item>
<item >UAE</item>
<item >XYZ</item>
</string-array>
Row.xml
在这个文件中我没有什么了不起的。它只包含两个文本视图。
日志猫
FATAL EXCEPTION: main
Process: com.threepin.deepakcorporation, PID: 14033
java.lang.NullPointerException
at com.threepin.deepakcorporation.AreaFragment$myAdapter.<init> (AreaFragment.java:73)
at com.threepin.deepakcorporation.AreaFragment.onCreateView(AreaFragment.java:38)
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:5127)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
首先在 onCreateView()
改变这个
list.setAdapter(new myAdapter(c));
到
list.setAdapter(new myAdapter(this.getActivity()));
因为 'c' 将为空。
接下来, 如上。
改变
for(int i=0;i<10;i++)
到
for(int i=0;i<oneofarray.length;i++)