使用ArrayLists的ListView中的错误

时间:2015-11-14 03:58:19

标签: java android

我有5个名为myMeatmyVegmyDairymyFruitmySauce的数组列表,其中包含一系列成分名称。

My XML code for the activity is as below

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.viv.droidchef.MyIngredients"
            tools:showIn="@layout/app_bar_my_ingredients">

            <TextView
                android:id="@+id/myVeg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="40dp"
                android:text="Vegetables:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/myVegList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myVeg" />

            <TextView
                android:id="@+id/myMeat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myVegList"
                android:text="Meat:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/myMeatList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myMeat" />

            <TextView
                android:id="@+id/mySpices"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myMeatList"
                android:text="Spices:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/mySpicesList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/mySpices" />

            <TextView
                android:id="@+id/mySauces"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/mySpicesList"
                android:text="Sauces:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/mySaucesList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/mySauces" />

            <TextView
                android:id="@+id/myDairy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/mySaucesList"
                android:text="Dairy:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/myDairyList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myDairy" />

            <TextView
                android:id="@+id/myFruits"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myDairyList"
                android:text="Fruits:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/myFruitsList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/myFruits" />
        </RelativeLayout>
    </ScrollView>

在打开活动时,它会崩溃。

现在在Java页面中,

public class MyIngredients extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    ArrayList<String> myVeg;
    ArrayList<String> myMeat;
    ArrayList<String> mySpices;
    ArrayList<String> mySauces;
    ArrayList<String> myDairy;
    ArrayList<String> myFruits;
    ListView vegList;
    ListView meatList;
    ListView spicesList;
    ListView saucesList;
    ListView dairyList;
    ListView fruitsList;


      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_ing);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

    Log.d("Cat", "1st Checkpoint!");
            addCheckedIng();
            Log.d("Cat", "4th Checkpoint!");
            vegList = (ListView) findViewById(R.id.myVegList);
            meatList = (ListView) findViewById(R.id.myMeatList);
            spicesList = (ListView) findViewById(R.id.mySpicesList);
            saucesList = (ListView) findViewById(R.id.mySaucesList);
            dairyList = (ListView) findViewById(R.id.myDairyList);
            fruitsList = (ListView) findViewById(R.id.myFruitsList);
            Log.d("Cat", "5th Checkpoint!");
            ArrayAdapter<String> veggies = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myVeg);
            vegList.setAdapter(veggies);
            Log.d("Cat", "6th Checkpoint!");
            ArrayAdapter<String> meat = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myMeat);
            vegList.setAdapter(meat);
            Log.d("Cat", "7th Checkpoint!");
            ArrayAdapter<String> spices = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mySpices);
            vegList.setAdapter(spices);
            Log.d("Cat", "8th Checkpoint!");
            ArrayAdapter<String> sauces = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mySauces);
            vegList.setAdapter(sauces);
            Log.d("Cat", "9th Checkpoint!");
            ArrayAdapter<String> fruits = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myFruits);
            vegList.setAdapter(fruits);


            Log.d("Cat", "FINAL Checkpoint!");
        }

arlaylists填写方法addCheckedIng();

这未在代码中显示。如果需要请评论并要求。

它只到达第5个检查点并且活动崩溃了,请帮帮我。

2 个答案:

答案 0 :(得分:4)

你因为myVeg,myMeat等为空而崩溃。在将ArrayList对象添加到适配器之前,需要实例化它们。

答案 1 :(得分:2)

请检查我的蔬菜清单是否为空。 在创建数组适配器的对象之前添加此代码。

myveg = new ArrayList<String>();
myveg.add("Vegetable");