毕加索 - 目标不得为空

时间:2018-01-13 15:40:58

标签: java android

每当新意图打开时,它会在该页面上停留一秒钟,并且应用程序将被强制关闭。有人帮我解决了如何调试此运行时错误。

public class FoodDetails extends AppCompatActivity {


ImageView img_food;

String foodId="";
FirebaseDatabase database;
DatabaseReference foods;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_food_details);

    database=FirebaseDatabase.getInstance();
    foods = database.getReference("foods");

    numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
    btncart = (FloatingActionButton)findViewById(R.id.btncart);
    food_description = (TextView)findViewById(R.id.food_description);
    food_name = (TextView)findViewById(R.id.food_name);
    food_price = (TextView)findViewById(R.id.food_price);
    img_food = (ImageView)findViewById(R.id.img_food);

    collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);

    collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExtendedAppbar);
    collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);

    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) {
            Food food = dataSnapshot.getValue(Food.class);
            Picasso.with(getBaseContext()).load(food.getImage())
                    .into(img_food);

            collapsingToolbarLayout.setTitle(food.getName());
            food_price.setText(food.getPrice());
            food_name.setText(food.getName());
            food_description.setText((food.getDescription()));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

错误的堆栈跟踪,这是错误所在的行 在com.k.menu.FoodDetails $ 1.onDataChange(FoodDetails.java:63)

    01-14 21:53:25.446 10015-10015/com.k.menu E/AndroidRuntime: FATAL EXCEPTION: main
                                                        Process: com.k.menu, PID: 10015
                                                        java.lang.IllegalArgumentException: Target must not be null.
                                                            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                                                            at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                            at com.k.menu.FoodDetails$1.onDataChange(FoodDetails.java:64)
                                                            at com.google.android.gms.internal.zzegf.zza(Unknown Source)
                                                            at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
                                                            at com.google.android.gms.internal.zzeig.run(Unknown Source)
                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                            at android.os.Looper.loop(Looper.java:154)
                                                            at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我的模特食品类

public class Food {

private String Name,Image,Description,Price,MenuId;

public Food() {
}

public Food(String name, String image, String description, String price, String menuId) {
    Name = name;
    Image = image;
    Description = description;
    Price = price;
    MenuId = menuId;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getImage() {
    return Image;
}

public void setImage(String image) {
    Image = image;
}

public String getDescription() {
    return Description;
}

public void setDescription(String description) {
    Description = description;
}

public String getPrice() {
    return Price;
}

public void setPrice(String price) {
    Price = price;
}

public String getMenuId() {
    return MenuId;
}

public void setMenuId(String menuId) {
    MenuId = menuId;
}

}

1 个答案:

答案 0 :(得分:1)

实际上,您没有获得任何数据的原因是您的数据库参考。在您的数据库结构中,您有一个名为“Foods”的孩子,因为您正在获取的孩子是“食物”。改变

foods = database.getReference("foods");

foods = database.getReference("Foods");