2

Please Help... Whenever I am trying to go to foodDetailActivity..it is throwing page back to the signup-signin activity?? When i debug the fooddetail activity..it is showing below mentioned 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);
    }
}

It is my 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) {

            }
        });
    }
}

It is showing error in "if(!foodId.isEmpty())"??

and it is showing below mentioned 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

help me to solve this..i am not getting what is wrong??

2

2 Answers 2

10

The error log is saying the code is trying to execute isEmpty() method on a NULL object. I see you have 2 places calling isEmpty(). Before the not empty check, check the object for not null first. Something like this:

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);

    }

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help!! It solved my debugging problem.. now the fooddetail activity is working only when i put this activity in launcher but when im trying to go with the flow of app..it is keep throwing page back to the signin-signup activity after the foodlist activity....and there is no logcat at all? means no error... why this is happening? Again thanks...
Couldn't tell from your provided code as there is no signup-signin activity. I would suggest you to add some debugging log to check the flow of your activities. Glad that I helped with your NullPointerException problem, why don't mark it as answer then :)
0

I can't see the "FoodId" in your code,and you should make sure "FoodId" is the same as another activity.

1 Comment

now, nullpointerexception solved...but it is keep throwing page back to the sign-signup activity after the foodlist activity? and there are no errors in the logcat...Is there any problem in the Intent?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.