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.