gpt4 book ai didi

android - 尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.isEmpty()'

转载 作者:行者123 更新时间:2023-12-05 08:41:34 25 4
gpt4 key购买 nike

请帮助...每当我尝试转到 foodDetailActivity.. 它会将页面返回到注册登录 Activity ?当我调试 fooddetail Activity 时..它显示下面提到的 logcat..

public class FoodList extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

FirebaseDatabase database;
DatabaseReference foodList;
String categoryId="";

FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter;

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

database=FirebaseDatabase.getInstance();
foodList=database.getReference("Foods");

recyclerView=findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

if(getIntent() !=null)
categoryId=getIntent().getStringExtra(CategoryId);
if(!categoryId.isEmpty() && categoryId !=null)
{
loadListFood(categoryId);
}
}

private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,R.layout.food_item,FoodViewHolder.class,
foodList.orderByChild("MenuId").equalTo(categoryId)) {
@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {

viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.food_image);

final Food local=model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodDetail=new Intent(FoodList.this,FoodDetail.class);
foodDetail.putExtra(FoodId,adapter.getRef(position).getKey());
startActivity(foodDetail);
}
});
}
};
Log.d("TAG",""+adapter.getItemCount());
recyclerView.setAdapter(adapter);
}
}

这是我的 FoodDetail Activity ...

public class FoodDetail extends AppCompatActivity {

TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;

String foodId="";

FirebaseDatabase database;
DatabaseReference foods;
Food currentFood;

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

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

numberButton = findViewById(R.id.number_button);
btnCart = findViewById(R.id.btnCart);
btnCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Database(getBaseContext()).addToCart(new Order(
foodId,
currentFood.getName(),
numberButton.getNumber(),
currentFood.getPrice(),
currentFood.getDiscount()
));

Toast.makeText(FoodDetail.this, "Added to Cart", Toast.LENGTH_SHORT).show();
}
});

food_description = findViewById(R.id.food_description);
food_name = findViewById(R.id.food_name);
food_price = findViewById(R.id.food_price);
food_image = findViewById(R.id.img_food);

collapsingToolbarLayout = findViewById(R.id.collapsing);

collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
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) {
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) {

}
});
}
}

它在“if(!foodId.isEmpty())”中显示错误??

它显示下面提到的 logcat..

02-14 11:49:21.238 6803-6803/com.example.rajatraturi.myrestaur E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rajatraturi.myrestaur, PID: 6803 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajatraturi.myrestaur/com.example.rajatraturi.myrestaur.FoodDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference at com.example.rajatraturi.myrestaur.FoodDetail.onCreate(FoodDetail.java:81) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)  at android.app.ActivityThread.access$900(ActivityThread.java:153)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5441)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)  02-14 11:49:21.292 6803-6803/com.example.rajatraturi.myrestaur I/Process: Sending signal. PID: 6803 SIG: 9

帮我解决这个问题..我没明白是怎么回事??

最佳答案

错误日志表明代码正在尝试对 NULL 对象执行 isEmpty() 方法。我看到您有 2 个地方调用 isEmpty()。在 not empty 检查之前,首先检查对象是否为 not null。像这样:

public class FoodList extends AppCompatActivity {
.....

@Override
protected void onCreate(Bundle savedInstanceState) {
.....

// check for not null first
if (categoryId !=null && !categoryId.isEmpty())
{
loadListFood(categoryId);
}
}

}


public class FoodDetail extends AppCompatActivity {
....

@Override
protected void onCreate(Bundle savedInstanceState) {
.....

// check for not null first
if (foodId != null && !foodId.isEmpty())
getDetailFood(foodId);

}

祝你好运!

关于android - 尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.isEmpty()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com