使用ArrayLists的ListView中出现错误

卡莉安·纳丁帕利

我称5周的ArrayList 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!");
        }

数组列表填充在方法中addCheckedIng();

该代码未显示。如果需要,请发表评论并提出要求。

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

eclipse1203

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用ArrayLists的ListView中出现错误

来自分类Dev

使用nullptr在Codeblocks IDE中出现错误

来自分类Dev

使用sql的Android Studio中出现错误

来自分类Dev

使用SQLite Swift的Singleton中出现错误

来自分类Dev

为什么在此ListView创建中出现错误?

来自分类Dev

getString()中出现错误

来自分类Dev

使用 '。' 使用属性路由的Web Api中出现404错误

来自分类Dev

使用Slug访问url时在Django中出现NoReverseMatch错误

来自分类Dev

使用LaunchControl运行时,bash脚本中出现错误

来自分类Dev

使用BigDecimal会导致计算中出现轻微错误

来自分类Dev

不使用SignalR时ELMAH中出现SignalR错误

来自分类Dev

使用泛型时在变量分配中出现错误

来自分类Dev

使用泛型时在变量分配中出现错误

来自分类Dev

使用Hibernate:3.6.10.15插件的Grails 2.4.0中出现错误

来自分类Dev

使用BigDecimal会导致计算中出现轻微错误

来自分类Dev

使用Openlayers的WFS请求中出现奇怪的错误

来自分类Dev

使用geom_errorbar时数据帧中出现错误?

来自分类Dev

使用Swift 3在do {行中出现预期的声明错误

来自分类Dev

为什么在使用Eclipse的python中出现语法错误?

来自分类Dev

使用URL模板标记后,在Django中出现错误/ NoReverseMatch

来自分类Dev

使用 Visual Studio 在 emgu cv 中出现 foreach 循环错误

来自分类Dev

使用 IIS Express 在 AngularJS 项目中出现 403 错误

来自分类Dev

使用 if 语句的过程中出现未知列错误

来自分类Dev

烧瓶中出现奇怪的错误:

来自分类Dev

程序中出现错误流浪“#”

来自分类Dev

在if语句中出现错误

来自分类Dev

通道中出现CoroutineContext错误

来自分类Dev

烧瓶中出现奇怪的错误:

来自分类Dev

程序中出现错误流浪“#”

Related 相关文章

热门标签

归档