-1

在我的项目中,我必须在一个普通的 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”很好,当我正常使用时适配器工作正常(没有适配器数组)..

谢谢

4

2 回答 2

2

myAdapters.add(0, adapter);在你的这一行onCreatemyAdapters是空的。

于 2013-03-15T14:36:23.763 回答
0

你可能想要一个ArrayList<MyAdapter> myAdapters = new ArrayList<MyAdapter>()在你打电话之前,myAdapters.add(0, adapter)否则myAdapters为空

于 2013-03-15T14:40:08.317 回答