-5

I have a text box and a button that adds to a Firebase databse and shows in a listview. When you click on an item in the listview it should bring you to a seperate activity where you can add more information. This does not happen, instead when I click add the application crashes with the error : "Unfortunately app has stopped"

Here is my code:

addFood activity :

public class addFood extends AppCompatActivity {

//we will use these constants later to pass the artist name and id to another activity
public static final String FOOD_NAME = "net.simplifiedcoding.firebasedatabaseexample.artistname";
public static final String FOOD_ID = "net.simplifiedcoding.firebasedatabaseexample.artistid";

//view objects
EditText editTextName;
Spinner spinnerCategory;
Button buttonAddFood;
ListView listViewFoods;

//a list to store all the foods from firebase database
List<Food> foods;

//our database reference object
DatabaseReference databaseFoods;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_food);

    //getting the reference of artists node
    databaseFoods = FirebaseDatabase.getInstance().getReference("foods");

    //getting views
    editTextName = (EditText) findViewById(R.id.editTextName);
    spinnerCategory = (Spinner) findViewById(R.id.spinnerCategories);
    listViewFoods = (ListView) findViewById(R.id.listViewFoods);

    buttonAddFood = (Button) findViewById(R.id.buttonAddFood);

    //list to store artists
    foods = new ArrayList<>();

    //adding an onclicklistener to button
    buttonAddFood.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //calling the method addArtist()
            //the method is defined below
            //this method is actually performing the write operation
            addFood();
        }
    });

    //attaching listener to listview
    listViewFoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //getting the selected artist
            Food food = foods.get(i);

            //creating an intent
            Intent intent = new Intent(getApplicationContext(), nutritionalInfo.class);

            //putting artist name and id to intent
            intent.putExtra(FOOD_ID, food.getFoodId());
            intent.putExtra(FOOD_NAME, food.getFoodName());

            //starting the activity with intent
            startActivity(intent);
        }
});}

protected void onStart() {
    super.onStart();
    //attaching value event listener
    databaseFoods.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clearing the previous artist list
            foods.clear();

            //iterating through all the nodes
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                //getting artist
                Food food = postSnapshot.getValue(Food.class);
                //adding artist to the list
                foods.add(food);
            }

            //creating adapter
            FoodList foodAdapter = new FoodList(addFood.this, foods);
            //attaching adapter to the listview
            listViewFoods.setAdapter(foodAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
/*
* This method is saving a new artist to the
* Firebase Realtime Database
* */
private void addFood() {
    //getting the values to save
    String name = editTextName.getText().toString().trim();
    String category = spinnerCategory.getSelectedItem().toString();

    //checking if the value is provided
    if (!TextUtils.isEmpty(name)) {

        //getting a unique id using push().getKey() method
        //it will create a unique id and we will use it as the Primary Key for our Artist
        String id = databaseFoods.push().getKey();

        //creating an Artist Object
        Food food = new Food(id, name, category);

        //Saving the Artist
        databaseFoods.child(id).setValue(food);

        //setting edittext to blank again
        editTextName.setText("");

        //displaying a success toast
        Toast.makeText(this, "food added", Toast.LENGTH_LONG).show();
    } else {
        //if the value is not given displaying a toast
        Toast.makeText(this, "Please enter a food", Toast.LENGTH_LONG).show();
    }
}

}

nutritionalInfo activity :

public class nutritionalInfo extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nutritional_information);
}

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.virtual.fridge">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".addFood">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

fhsks
  • 15
  • 1
  • 6
  • 3
    And should we guess the Exception ? Add the stacktrace of crash. – ADM Jan 17 '18 at 15:10
  • Seems like shortages in reading with understanding ... logcat prolly contains "have you declared this activity in your AndroidManifest.xml?" – Selvin Jan 17 '18 at 15:22

1 Answers1

2

You need to register nutritionalInfo activity in manifest as

<activity android:name=".nutritionalInfo"/>
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Worked thank you..will i always have to do this manually for each activity or should it do it automatically? – fhsks Jan 17 '18 at 15:15
  • if you add new activity using `right click(on package ) -> new Activity` then no, otherwise yes – Pavneet_Singh Jan 17 '18 at 15:16
  • thank you, if i add a layout resource file should i add it to the manifest too? – fhsks Jan 17 '18 at 16:09
  • @fhsks NO, you need to add some specific components and additional information tags as [**mentioned here**](https://developer.android.com/guide/topics/manifest/manifest-intro.html#filestruct) – Pavneet_Singh Jan 17 '18 at 16:12