Android应用程序在活动之间移动时崩溃
我有一个文本框和一个按钮,可以添加到Firebase数据库并显示在列表视图中。当您在listview中单击某个项目时,它会将您带到一个单独的活动,您可以在其中添加更多信息。这不会发生,相反,当我单击“添加”时,应用程序崩溃并出现错误:“不幸的是,应用程序已停止” 这是我的密码: 添加食物活动:Android应用程序在活动之间移动时崩溃,android,firebase,Android,Firebase,我有一个文本框和一个按钮,可以添加到Firebase数据库并显示在列表视图中。当您在listview中单击某个项目时,它会将您带到一个单独的活动,您可以在其中添加更多信息。这不会发生,相反,当我单击“添加”时,应用程序崩溃并出现错误:“不幸的是,应用程序已停止” 这是我的密码: 添加食物活动: public class addFood extends AppCompatActivity { //we will use these constants later to pass the arti
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();
}
}
}
舱单:
<?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>
您需要在
清单
中注册营养信息
活动
<activity android:name=".nutritionalInfo"/>
我们应该猜测例外情况吗?加上崩溃的痕迹。似乎在阅读和理解方面存在不足。。。logcat prolly包含“您是否在AndroidManifest.xml中声明了此活动?”谢谢。我是否总是必须为每个活动手动执行此操作,还是应该自动执行此操作?如果您使用右键单击(在包上)->新活动添加新活动,则否,否则,谢谢,如果我添加一个布局资源文件,我是否也应该将其添加到清单中?@fhsks否,您需要添加一些特定组件和其他信息标记,如下所示:
<activity android:name=".nutritionalInfo"/>