【发布时间】:2013-03-04 07:42:03
【问题描述】:
在我的项目中,我必须在一个普通的Activity中设置多个ListView(这些ListView的数量取决于一个ArrayList的大小)。
所以我必须以编程方式动态添加 ListView 和自定义 ArrayAdapter。
但我遇到的主要问题是当我尝试做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”很好,当我正常使用时适配器工作正常(没有适配器数组)..
谢谢
【问题讨论】:
-
第 43 行是什么?如果没有完整的文件,我们真的无法猜测。但是该行的某些内容正在取消引用 null。
标签: android arraylist android-asynctask nullpointerexception android-arrayadapter