Java ListView中使用ArrayList时出错

Java ListView中使用ArrayList时出错,java,android,Java,Android,我有5个数组列表,分别是myMeat,myVeg,myDairy,myFruit,mySauce,它们都有一系列的成分名称 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="

我有5个数组列表,分别是
myMeat
myVeg
myDairy
myFruit
mySauce
,它们都有一系列的成分名称

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>
我的活动XML代码如下所示
打开活动时,它会崩溃

现在在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!");
        }
公共类MyComponents扩展AppCompative活动
实现NavigationView.OnNavigationItemSelectedListener{
ArrayList myVeg;
ArrayList myMeat;
ArrayList mySpices;
阿雷利斯特·迈苏克斯;
ArraylistMyDairy;
排果;
列表视图;
列表视图列表;
ListView spicesList;
ListView酱汁列表;
ListView奶牛场;
列表视图水果列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
设置内容视图(R.layout.my_ing);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Snackbar.make(查看“替换为您自己的操作”,Snackbar.LENGTH\u LONG)
.setAction(“Action”,null).show();
}
});
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
Log.d(“Cat”,“第一个检查点!”);
addChecking();
Log.d(“Cat”,“第四个检查点!”);
vegList=(ListView)findViewById(R.id.myVegList);
meatList=(ListView)findViewById(R.id.myMeatList);
spicesList=(ListView)findViewById(R.id.mySpicesList);
SauceList=(ListView)findViewById(R.id.mySauceList);
dairyList=(ListView)findViewById(R.id.myDairyList);
水果列表=(ListView)findViewById(R.id.MyFroutsList);
Log.d(“Cat”,“第五检查点!”);
ArrayAdapter veggies=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,myVeg);
vegList.setAdapter(蔬菜);
Log.d(“Cat”,“第6个检查点!”);
ArrayAdapter meat=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,myMeat);
蔬菜列表.肉;
Log.d(“Cat”,“第7个检查点!”);
ArrayAdapter spices=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,mySpices);
蔬菜列表。setAdapter(香料);
Log.d(“Cat”,“第8个检查点!”);
ArrayAdapter sauces=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,mySauces);
蔬菜列表:调味料;
Log.d(“Cat”,“第9个检查点!”);
ArrayAdapter fruits=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,myFruits);
蔬菜列表。setAdapter(水果);
Log.d(“Cat”,“最终检查点!”);
}
数组列表在方法
addCheckedIng();
中填写

代码中未显示此信息。如果需要,请评论并索取


它只到达第5个检查点,活动崩溃,请帮助我解决。

您崩溃是因为myVeg、myMeat等为空。您需要实例化ArrayList对象,然后再将它们添加到适配器。

请检查您的my veg列表。它为空。 在创建数组适配器的对象之前添加此代码

myveg = new ArrayList<String>();
myveg.add("Vegetable");
myveg=newarraylist();
我的蔬菜添加(“蔬菜”);

很抱歉,我是一个初学者,不知道stacktrace是什么。感谢您6次指出veglist上的适配器,是的,我的活动中有6个列表视图。这是一个问题吗?嗯,您能提供一些示例代码说明如何做到这一点吗?myVeg=new ArrayList();然后是myVeg.add(“veg name”);ArrayList myVeg,myMeat,mypices,myauces,myDairy,myFruits=new ArrayList();不,您需要单独执行。ArrayList myVeg=new arrarylist();然后ArrayList myMeat=new arrarylist();对于每个ArrayList myVeg,myMeat,mypices,myauces,myDairy,myFruits=new ArrayList();我在课程开始时添加了它。请在数组列表中添加一些项,如myveg.add(“蔬菜”);在创建listview之前会调用此方法。在此方法中,会添加成分。