我是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
在这个文件中我什么都没有。它只包含两个textView。
登录CAT
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$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: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)
答案 0 :(得分:0)
首先在onCreateView()
更改此
list.setAdapter(new myAdapter(c));
到
list.setAdapter(new myAdapter(this.getActivity()));
因为&#39; c&#39;将为空。
接下来, 如上所述。
变化
for(int i=0;i<10;i++)
到
for(int i=0;i<oneofarray.length;i++)