在我的项目中我必须设置一个普通的Activity多个ListView(这些ListView的数量取决于ArrayList的大小)。
所以我必须以编程方式动态添加ListView和Custom ArrayAdapter。
我有一个想法让AsyncTask做这些脏东西:制作一个自定义ArrayAdapter数组,并为数组中的每个Adapter动态设置ListView。
但我遇到的主要问题是当我尝试做MyAdapterArray.add(MyAdapter)
时......
我有一个空指针异常:
03-15 15:24:21.738: E/AndroidRuntime(21672): FATAL EXCEPTION: main
03-15 15:24:21.738: E/AndroidRuntime(21672): java.lang.RuntimeException: Unable to start activity ComponentInfo{azur.mobile.incomrestau/azur.mobile.incomrestau.SousCarteActivity}: java.lang.NullPointerException
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.os.Looper.loop(Looper.java:137)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-15 15:24:21.738: E/AndroidRuntime(21672): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 15:24:21.738: E/AndroidRuntime(21672): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 15:24:21.738: E/AndroidRuntime(21672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-15 15:24:21.738: E/AndroidRuntime(21672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-15 15:24:21.738: E/AndroidRuntime(21672): at dalvik.system.NativeStart.main(Native Method)
03-15 15:24:21.738: E/AndroidRuntime(21672): Caused by: java.lang.NullPointerException
03-15 15:24:21.738: E/AndroidRuntime(21672): at azur.mobile.incomrestau.SousCarteActivity.onCreate(SousCarteActivity.java:43)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.Activity.performCreate(Activity.java:5206)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-15 15:24:21.738: E/AndroidRuntime(21672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-15 15:24:21.738: E/AndroidRuntime(21672): ... 11 more
我的活动在这里:
public class SousCarteActivity extends Activity {
private String nom_categorie;
private ArrayList<String> arraySousCategoriesName;
private ArrayList<ArrayList<ElementFood>> arraySousCategories;
private ArrayList<MyAdapter> myAdapters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_souscarte_list_item);
nom_categorie = getIntent().getStringExtra("nom_categorie");
FoodsContainer.setSousCategoriesNameArray(nom_categorie);
FoodsContainer.setSousCategoriesArray(nom_categorie);
arraySousCategories = FoodsContainer.sousCategoriesArray;
MyAdapter adapter = new MyAdapter(this, arraySousCategories.get(0));
myAdapters.add(0, adapter);
//new DoDirtyJobAsyncTask().execute();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private class DoDirtyJobAsyncTask extends AsyncTask<Void, MyAdapter, Void> {
@Override
protected Void doInBackground(Void... params) {
for (ArrayList<ElementFood> arrayElement : arraySousCategories) {
MyAdapter myAdapter = new MyAdapter(getApplicationContext(), arrayElement);
myAdapters.add(myAdapter);
publishProgress(myAdapter);
}
return null;
}
@Override
protected void onProgressUpdate(MyAdapter... myAdapters) {
int currViewId = 1;
for (final MyAdapter myAdapter: myAdapters) {
ListView listview = new ListView(getApplicationContext(), null);
listview.setId(currViewId);
listview.setAdapter(myAdapter);
LinearLayout ll = (LinearLayout) findViewById(R.id.souscarte_linearlayout);
ll.addView(listview);
currViewId++;
}
}
}
class MyAdapter extends ArrayAdapter<ElementFood>
{
LayoutInflater inflat;
private ArrayList<ElementFood> items;
public MyAdapter(Context context, ArrayList<ElementFood> objects)
{
super(context, R.layout.activity_souscarte_list_item_elementsouscategorie, objects);
this.items = objects;
this.inflat = LayoutInflater.from(context);
}
private class ViewHolder{
public TextView title;
public TextView prix;
public TextView desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflat.inflate(R.layout.activity_souscarte_list_item_elementsouscategorie, null);
holder.title = (TextView) convertView.findViewById(R.id.souscarte_element_title);
holder.prix = (TextView) convertView.findViewById(R.id.souscarte_element_prix);
holder.desc = (TextView) convertView.findViewById(R.id.souscarte_element_desc);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ElementFood element = items.get(position);
if (element != null) {
holder.title.setText(element.getName());
holder.prix.setText(Float.toString(element.getPrice()));
holder.desc.setText(element.getDescription());
}
return convertView;
}
}
}
PS:我的阵列“arraySousCategories”很好,当我正常使用它时,适配器工作正常(没有适配器阵列)..
谢谢
答案 0 :(得分:2)
在myAdapters.add(0, adapter);
的{{1}}行,onCreate
为空。
答案 1 :(得分:0)
您可能需要ArrayList<MyAdapter> myAdapters = new ArrayList<MyAdapter>()
才能致电myAdapters.add(0, adapter)
,否则myAdapters
为空