Application Crashing and not continuing to layout_2 when Continue button is clicked (Android Studio)

There are no errors in the java, but when I try to run my code in my phone (I had to run it in my phone because my Android Studio is so slow) it’s crashing. I will provide some of my codes in my java.

package com.vlsqz.activity2;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Spinner spinner;

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

        spinner = findViewById(R.id.spinner);

        // Populate spinner with options (Samsung and Apple)
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_items, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        // Handle continue button click
        findViewById(R.id.continue_button).setOnClickListener(view -> {
            String selectedItem = spinner.getSelectedItem().toString();

            Intent intent = new Intent(MainActivity.this, layout_2.class);
            intent.putExtra("selectedItem", selectedItem);
            startActivity(intent);
        });
    }
}

package com.vlsqz.activity2;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class layout_2 extends AppCompatActivity {

    private GridLayout gridLayout;

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

        // Retrieve selected item from Intent extras
        String selectedItem = getIntent().getStringExtra("selectedItem");

        // Log the selectedItem to see if it's received correctly
        Log.d("Layout_2", "Selected Item: " + selectedItem);

        // Initialize GridLayout
        gridLayout = findViewById(R.id.grid_layout);

        // Check if gridLayout is null
        if(gridLayout == null) {
            Log.e("Layout_2", "GridLayout is null");
            return;
        }

        // Populate GridLayout based on the selected item
        if (selectedItem.equals("Samsung")) {
            addProducts(getSamsungProducts());
        } else {
            // If selectedItem is not "Samsung", handle it accordingly
            // For example, you can display a message or do nothing
            Log.e("Layout_2", "Invalid selected item: " + selectedItem);
        }
    }



    private void addProducts(Product[] products) {
        for (int i = 0; i < products.length; i++) {
            // Find the IDs dynamically based on the item index
            int itemId = getResources().getIdentifier("item" + (i + 1) + "_layout", "id", getPackageName());
            GridLayout itemLayout = gridLayout.findViewById(itemId);

            // Find other view IDs within the item layout
            ImageView imageView = itemLayout.findViewById(getResources().getIdentifier("image" + (i + 1), "id", getPackageName()));
            TextView nameTextView = itemLayout.findViewById(getResources().getIdentifier("itemName" + (i + 1), "id", getPackageName()));
            TextView priceTextView = itemLayout.findViewById(getResources().getIdentifier("itemPrice" + (i + 1), "id", getPackageName()));
            EditText qtyEditText = itemLayout.findViewById(getResources().getIdentifier("editQty" + (i + 1), "id", getPackageName()));
            Button decButton = itemLayout.findViewById(getResources().getIdentifier("dec" + (i + 1), "id", getPackageName()));
            Button incButton = itemLayout.findViewById(getResources().getIdentifier("inc" + (i + 1), "id", getPackageName()));
            Button addButton = itemLayout.findViewById(getResources().getIdentifier("add" + (i + 1), "id", getPackageName()));

            // Set product data to views
            imageView.setImageResource(products[i].getImageResId());
            nameTextView.setText(products[i].getName());
            priceTextView.setText(products[i].getPrice());
            qtyEditText.setText("1"); // Assuming default quantity is 1

            // Add click listeners to buttons if needed

            // Add the item layout to the GridLayout
            gridLayout.addView(itemLayout);
        }
    }

    // Dummy method to get Samsung products
    private Product[] getSamsungProducts() {
        return new Product[]{
                new Product(R.drawable.galaxy_s24_ultra, "Galaxy S24 Ultra", "₱87,990.00"),
                new Product(R.drawable.galaxy_tab_s9, "Galaxy Tab S9 Ultra 5G", "₱85,990.00"),
                new Product(R.drawable.galaxy_watch_6, "Galaxy Watch 6", "₱18,990.00"),
                new Product(R.drawable.galaxy_buds, "Galaxy Buds 2 Pro", "₱12,990.00"),
                // Add more Samsung products as needed
        };
    }

    // Dummy method to get Apple products
    private Product[] getAppleProducts() {
        return new Product[]{
                new Product(R.drawable.macbook_pro, "MacBook Pro", "₱104,990.00"),
                new Product(R.drawable.iphone_15_pro, "iPhone 15 Pro Max", "₱70,990.00"),
                new Product(R.drawable.ipad_pro, "iPad Pro", "₱55,990"),
                new Product(R.drawable.apple_watch, "Apple Watch Ultra 2", "₱54,990.00"),
                // Add more Apple products as needed
        };
    }
}


I want to display all the items inside the layout_2.xml and want to run it, but it keeps crashing as I clicked Continue button layout_2.xml

New contributor

Primo Kenshin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật