Solution 1 :
I am guessing you need the button to be created when the activity pop_cart.java is in the foreground.
In that case why don’t you directly execute the button code in the onCreate() of pop_cart.java?
You can pass a boolean from pop_accompaniments.java to pop_cart.java via an intent if you want something in pop_accompaniments.java to decide whether the button will be created or not!
In pop_accompaniments.java:
Intent intent = new Intent(this, pop_cart.class);
intent.putExtra("someBool", true);
In pop_cart.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
Boolean bool = getIntent().getExtras().getBoolean("someBool");
//Now you can check if the bool is true or false
if(bool){
//Execute button code here
}
}
Problem :
I want to add a button from 1 activity – “pop_accompaniments.xml” to another activity – “pop_cart.xml“, using java code. For this I created an instance of the class “pop_cart.java” in “pop_accompaniments.java“, to call the func – “cart_button_creator” which is in “pop_cart.java“.
But my app just crashes as soon as I open it.
pop_accompaniments.java
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class pop_accompaniments extends AppCompatActivity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_accompaniments);
Button assorted_condiments = findViewById(R.id.assorted_condiments);
}
@Override
public void onClick(View v) {
setContentView(R.layout.pop_cart);
pop_cart main = new pop_cart();
int id;
String name;
Button myButton;
switch (v.getId()) {
case R.id.assorted_condiments:
myButton = new Button(this);
name = "Assorted Condiments";
id = generateButtonId();
main.cart_content_keeper(id, name, myButton);
}
}
}
pop_cart.java
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class pop_cart extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.pop_cart);
}
public void cart_button_creator(String name, int id, Button myButton) {
//setContentView(R.layout.pop_cart);
LinearLayout ll = findViewById(R.id.cart_items);
myButton.setText(name);
myButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
myButton.setId(id);
ll.addView(myButton);
}
public void cart_content_keeper(int id, String name, final Button myButton) {
int n = 0;
int[] id_arr = {};
String[] name_arr = {};
//int price = 0;
name_arr = add_item(n, name_arr, name);
id_arr = add_id(n, id_arr, id);
n = n + 1;
Button refresh = findViewById(R.id.refresh);
final int finalN = n;
final String[] finalName_arr = name_arr;
final int[] finalId_arr = id_arr;
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.refresh) {
for (int i = 0; i < finalN; i++) {
cart_button_creator(finalName_arr[i], finalId_arr[i], myButton);
}
}
}
});
}
public static String[] add_item(int n, String[] arr, String x)
{
int i;
String[] newarr = new String[n + 1];
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
public static int[] add_id(int n, int[] arr, int x)
{
int i;
int[] newarr = new int[n + 1];
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
}
Sorry for such a long code but I have tried almost all the possible ways of calling a func from an instance of another class, passing the variables in a different manner, trying to call the whole variable generator as a function and only passing the view v as an argument in pop_accompaniments.java but the app keeps in crashing either when I click on the button “ASSORTED CONDIMENTS” at the very first step or if I just start the app it crashes.
Also, the buttons are created just fine if I am creating them in the same activity that is “pop_accompaniments.xml” while calling the button_creator method from “pop_accompaniments.java“.
But I want them to show in the CART activity not the ACCOMPANIMENTS activity.
Comments
Comment posted by Vipul Bhardwaj
No still the same error persists. The app is crashing as soon as it is launched. It doesn’t even open. I have checked and there are no other errors in my code that android studio my highlight as red or even yellow warnings.
Comment posted by Harsh Savergaonkar
Can you please share the log when you run the app? That way I will be able to help you better.