我正在使用Firebase数据库和Android Studio。当我运行应用程序并单击添加食物按钮时,应用程序崩溃并且没有任何内容添加到数据库中。如果我没有输入食物名称,则会按预期显示错误消息。
这是我的代码:
public class addFood extends AppCompatActivity {
//source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
EditText editTextName;
EditText editTextBestBefore;
Button buttonAddFood;
Spinner spinnerCategory;
DatabaseReference foodDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_food);
// source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
foodDatabase = FirebaseDatabase.getInstance().getReference("food");
editTextName =(EditText)(findViewById(R.id.editTextName));
editTextBestBefore =(EditText)(findViewById(R.id.editTextBestBefore));
buttonAddFood =(Button) (findViewById(R.id.buttonAddFood));
spinnerCategory =(Spinner) (findViewById(R.id.spinnerCategory));
buttonAddFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFood();
}
});
}
private void addFood()
//source : https://www.youtube.com/watch?v=EM2x33g4syY&t=415s
{
String foodName = editTextName.getText().toString().trim();
String bestBefore = editTextBestBefore.getText().toString().trim();
String category = spinnerCategory.getSelectedItem().toString();
if (!TextUtils.isEmpty(foodName)){
String id = foodDatabase.push().getKey();
food food = new food(id,foodName, bestBefore, category);
foodDatabase.child(id).setValue(food);
Toast.makeText(this, "food added!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Please enter a food name", Toast.LENGTH_LONG).show();
}
}
}
Logcat:
01-16 15:28:57.972 27506-27506 / com.virtual.a2018fridge I / FA:App测量正在启动,版本:11020 01-16 15:28:57.973 27506-27506 / com.virtual.a2018fridge I / FA:启用调试日志记录运行:adb shell setprop log.tag.FA VERBOSE 01-16 15:28:58.032 27506-27506 / com.virtual.a2018fridge V / FA:已启用收藏 01-16 15:28:58.033 27506-27506 / com.virtual.a2018fridge V / FA:应用程序包,谷歌应用程序ID:com.virtual.a2018fridge,1:203952876063:android:ae6871bb1651394b 01-16 15:28:58.035 27506-27506 / com.virtual.a2018fridge I / FA:要启用更快的调试模式事件记录运行: adb shell setprop debug.firebase.analytics.app com.virtual.a2018fridge 01-16 15:28:58.035 27506-27506 / com.virtual.a2018fridge D / FA:启用调试级消息记录 01-16 15:28:58.145 27506-27506 / com.virtual.a2018fridge V / FA:已注册的活动生命周期回调 01-16 15:28:58.272 27506-27625 / com.virtual.a2018fridge V / FA:使用测量服务 01-16 15:28:58.287 27506-27625 / com.virtual.a2018fridge V / FA:连接到远程服务 01-16 15:28:58.348 27506-27625 / com.virtual.a2018fridge V / FA:使用测量服务 01-16 15:28:58.348 27506-27625 / com.virtual.a2018fridge V / FA:连接尝试已在进行中 01-16 15:28:59.474 27506-27506 / com.virtual.a2018fridge V / FA:onActivityCreated 01-16 15:29:01.984 27506-27625 / com.virtual.a2018fridge V / FA:使用测量服务 01-16 15:29:01.984 27506-27625 / com.virtual.a2018fridge V / FA:连接尝试已在进行中 01-16 15:29:01.989 27506-27625 / com.virtual.a2018fridge V / FA:活动恢复,时间:329370286 01-16 15:29:01.993 27506-27625 / com.virtual.a2018fridge I / FA:未找到标记管理器,因此不会使用 01-16 15:29:02.001 27506-27625 / com.virtual.a2018fridge D / FA:记录事件(FE):screen_view(_vs),Bundle [{firebase_event_origin(_o)= auto,firebase_screen_class(_sc)= addFood,firebase_screen_id (_si)= 3652847265616890340}] 01-16 15:29:02.030 27506-27625 / com.virtual.a2018fridge V / FA:使用测量服务 01-16 15:29:02.030 27506-27625 / com.virtual.a2018fridge V / FA:连接尝试已在进行中 01-16 15:29:03.231 27506-27625 / com.virtual.a2018fridge D / FA:连接到远程服务 01-16 15:29:03.232 27506-27625 / com.virtual.a2018fridge V / FA:处理排队的服务任务:4 01-16 15:29:03.981 17286-27703 /? V / FA-SVC:记录事件:origin = auto,name = screen_view(_vs),params = Bundle [{firebase_event_origin(_ o)= auto,firebase_screen_class(_sc)= addFood,firebase_screen_id(_si)= 3652847265616890340}] 01-16 15:29:04.047 17286-27703 /? V / FA-SVC:保存事件,名称,数据大小:screen_view(_vs),52 01-16 15:29:04.057 17286-27703 /? V / FA-SVC:事件记录:事件{appId ='com.virtual.a2018fridge',name ='screen_view(_vs)',params = Bundle [{firebase_event_origin(_o)= auto,firebase_screen_class(_sc)= addFood,firebase_screen_id (_si)= 3652847265616890340}]} 01-16 15:29:04.063 17286-27703 /? V / FA-SVC:上传时间大约为ms:978689 01-16 15:29:04.097 17286-27703 /? V / FA-SVC:使用GcmTaskService调度上传 01-16 15:29:04.098 17286-27703 /? V / FA-SVC:使用Gcm调度任务。 Time978689 01-16 15:29:04.106 17286-27703 /? V / FA-SVC:后台事件处理时间,ms:133 01-16 15:29:07.348 27772-27772 /? I / FA:应用程序测量正在启动,版本:11020 01-16 15:29:07.349 27772-27772 /? I / FA:启用调试日志记录运行:adb shell setprop log.tag.FA VERBOSE 01-16 15:29:07.370 27772-27772 /? V / FA:已启用收藏 01-16 15:29:07.371 27772-27772 /? V / FA:应用程序包,谷歌应用程序ID:com.whatsapp,1:293955441834:android:7373a2d0bdfa3228 01-16 15:29:07.372 27772-27772 /? I / FA:要启用更快的调试模式事件记录运行: adb shell setprop debug.firebase.analytics.app com.whatsapp 01-16 15:29:07.372 27772-27772 /? D / FA:启用调试级别消息日志记录 01-16 15:29:07.388 27772-27772 /? V / FA:已注册的活动生命周期回调 01-16 15:29:07.415 27772-27818 /? V / FA:使用测量服务 01-16 15:29:07.419 27772-27818 /? V / FA:连接到远程服务 01-16 15:29:07.439 27772-27818 /? V / FA:使用测量服务 01-16 15:29:07.439 27772-27818 /? V / FA:连接尝试已在进行中 01-16 15:29:08.026 27772-27818 /? D / FA:连接到远程服务 01-16 15:29:08.026 27772-27818 /? V / FA:处理排队的服务任务:2 01-16 15:29:09.150 27506-27625 / com.virtual.a2018fridge V / FA:不活动,断开与服务的连接
[ 01-16 15:29:09.339 27506:27506 W/ ]
Unable to open '/system/framework/prcui-config.jar': No such file or directory
01-16 15:29:11.535 17286-27791 /? D / FA-SVC:包被替换意图:com.virtual.a2018fridge
答案 0 :(得分:1)
您没有添加构建数据库的代码
就像这样的事情
mRootRef.child("Chat").child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChild(mChatUser)){
Map chatAddMap = new HashMap();
chatAddMap.put("seen", false);
chatAddMap.put("timestamp", ServerValue.TIMESTAMP);
Map chatUserMap = new HashMap();
chatUserMap.put("Chat/" + mCurrentUserId + "/" + mChatUser, chatAddMap);
chatUserMap.put("Chat/" + mChatUser + "/" + mCurrentUserId, chatAddMap);
mRootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError != null){
Log.d("CHAT_LOG", databaseError.getMessage().toString());
}
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mChatSendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
您必须根据您的要求构建数据库