我是Android编程的新手,我试图创建这个应用程序,但我总是卡住并遇到这个问题,
E/Environment: initForCurrentUser:userId= 0
任何人都可以帮助我吗?这是我的代码,
Database.java
package com.example.acer.drcoffee;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle; import
android.view.View; import android.widget.ImageView; import
android.widget.TextView; import android.widget.Toast;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton; import
com.example.acer.drcoffee.Database.Database; import
com.example.acer.drcoffee.Model.Food; import
com.example.acer.drcoffee.Model.Order; import
com.google.firebase.database.DataSnapshot; import
com.google.firebase.database.DatabaseError; import
com.google.firebase.database.DatabaseReference; import
com.google.firebase.database.FirebaseDatabase; import
com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btn_Cart;
ElegantNumberButton number_Button;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
Food currentFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//init Firebase
database = FirebaseDatabase.getInstance();
foods = database.getReference("Foods");
//init View
number_Button = (ElegantNumberButton)findViewById(R.id.numberButton);
btn_Cart = (FloatingActionButton)findViewById(R.id.btnCart);
btn_Cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentFood.getName(),
number_Button.getNumber(),
currentFood.getPrice(),
currentFood.getDiscount()
));
Toast.makeText(FoodDetail.this,"Telah Ditambahkan !!",Toast.LENGTH_SHORT).show();
}
});
food_name = (TextView)findViewById(R.id.foodName);
food_price = (TextView)findViewById(R.id.foodPrice);
food_description = (TextView)findViewById(R.id.foodDescription);
food_image = (ImageView)findViewById(R.id.imgFood);
//Buat style untuk appbar di value/style
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
//get food ID from Intent
if (getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if (!foodId.isEmpty())
{
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentFood = dataSnapshot.getValue(Food.class);
//set image
Picasso.with(getBaseContext()).load(currentFood.getImage())
.into(food_image);
collapsingToolbarLayout.setTitle(currentFood.getName());
food_price.setText(currentFood.getPrice());
food_name.setText(currentFood.getName());
food_description.setText(currentFood.getDescription());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Order.java
package com.example.acer.drcoffee.Model;
/** * Created by acer on 09/12/2017. */
public class Order { private String ProductId; private String ProductName; private String Quantity; private String Price; private String Discount;
public Order() {
}
public Order(String productId, String productName, String quantity, String price, String discount) {
ProductId = productId;
ProductName = productName;
Quantity = quantity;
Price = price;
Discount = discount;
}
public String getProductId() {
return ProductId;
}
public void setProductId(String productId) {
ProductId = productId;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getQuantity() {
return Quantity;
}
public void setQuantity(String quantity) {
Quantity = quantity;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getDiscount() {
return Discount;
}
public void setDiscount(String discount) {
Discount = discount;
}
}
FoodDetail.java
package com.example.acer.drcoffee;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.ImageView; import android.widget.TextView;
import android.widget.Toast;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.acer.drcoffee.Database.Database;
import com.example.acer.drcoffee.Model.Food;
import com.example.acer.drcoffee.Model.Order;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btn_Cart;
ElegantNumberButton number_Button;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
Food currentFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//init Firebase
database = FirebaseDatabase.getInstance();
foods = database.getReference("Foods");
//init View
number_Button = (ElegantNumberButton)findViewById(R.id.numberButton);
btn_Cart = (FloatingActionButton)findViewById(R.id.btnCart);
btn_Cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentFood.getName(),
number_Button.getNumber(),
currentFood.getPrice(),
currentFood.getDiscount()
));
Toast.makeText(FoodDetail.this,"Telah Ditambahkan !!",Toast.LENGTH_SHORT).show();
}
});
food_name = (TextView)findViewById(R.id.foodName);
food_price = (TextView)findViewById(R.id.foodPrice);
food_description = (TextView)findViewById(R.id.foodDescription);
food_image = (ImageView)findViewById(R.id.imgFood);
//Buat style untuk appbar di value/style
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
//get food ID from Intent
if (getIntent() != null)
foodId = getIntent().getStringExtra("FoodId");
if (!foodId.isEmpty())
{
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentFood = dataSnapshot.getValue(Food.class);
//set image
Picasso.with(getBaseContext()).load(currentFood.getImage())
.into(food_image);
collapsingToolbarLayout.setTitle(currentFood.getName());
food_price.setText(currentFood.getPrice());
food_name.setText(currentFood.getName());
food_description.setText(currentFood.getDescription());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
我试图自己解决这个问题,但已经有2个星期了,我仍然不知道如何解决它,所以请任何人帮助我。 谢谢
答案 0 :(得分:0)
也许你应该重新创建你的项目。创建新项目然后逐个创建每个类并在创建每个类时运行应用程序,或者您可以设置断点并调试项目。也看看这个链接它可能对你有所帮助。 This link represent the class that initiate the current user