2

I have 5 ArrayLists called myMeat, myVeg, myDairy, myFruit, mySauce which all have a bunch of ingredients names in them.

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>

On opening the activity it crashes.

Now in the Java page,

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!");
        }

The arraylists are filled in the method addCheckedIng();.

This hasn't been shown in the code. If required please comment and ask for it.

It only reaches the 5th checkpoint and the activity crashes, please help me out.

2
  • I'm sorry I'm a beginner and don't know what a stacktrace is. Commented Nov 14, 2015 at 4:02
  • Thanks for pointing out the adapter on veglist 6 times and yes there are 6 listviews in my activity. Is that a problem? Commented Nov 14, 2015 at 4:03

2 Answers 2

4

You're crashing because myVeg, myMeat, etc are null. You need to instantiate your ArrayList objects before adding them to you adapter.

Sign up to request clarification or add additional context in comments.

7 Comments

Um could you provide some sample code of how to do that?
myVeg = new ArrayList<String>();
Then myVeg.add("veg name");
ArrayList<String> myVeg, myMeat, mySpices, mySauces, myDairy, myFruits = new ArrayList<String>();
No, you need to do them individually. ArrayList<String> myVeg = new ArrayList<String>(); then ArrayList<String> myMeat = new ArrayList<String>(); for each one
|
2

Please Check your my veg list it is null. Add this code before you make an object of array adapter.

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

4 Comments

ArrayList<String> myVeg, myMeat, mySpices, mySauces, myDairy, myFruits = new ArrayList<String>();
I have added that in the starting of the class.
Please add some items in an array list like myveg.add("Vegetable");
This method is being called before the listview is made. In this method the ingredients are added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.