1

I am making a donation app. There is a amount picker where the user can choose the donate amount. And there is also a textView where the total sum donated can be seen if the user donates multiple times.

I'm trying to implement a donation history where each item in the list is $X. I used a list using a fragment on the same screen, using a pre-defined string array works perfectly well.

see image here, sorry I don't have enough rep to embed

My app crashed when I tried to do pass the donated amount to the fragment each time the user clicks on the donate button.

I can't figure out what's the problem. I'm guessing maybe the fragment needs to refresh with the new data, or maybe I'm passing data the wrong way. I'm completely stuck, not sure how to fix this. Any help is greatly appreciated, thanks!

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button donateButton;
    private RadioGroup paymentMethod;
    private ProgressBar progressBar;
    private NumberPicker amountPicker;
    private TextView totalTextView;

    private int totalDonated = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        donateButton = (Button) findViewById(R.id.donateButton);

        if (donateButton != null)
        {
            Log.v("Donate", "Really got the donate button");
        }

        paymentMethod = (RadioGroup)   findViewById(R.id.paymentMethod);
        progressBar   = (ProgressBar)  findViewById(R.id.progressBar);
        amountPicker  = (NumberPicker) findViewById(R.id.amountPicker);
        totalTextView = (TextView) findViewById(R.id.totalTextView);

        // set the min and max values for the amount picker
        amountPicker.setMinValue(0);
        amountPicker.setMaxValue(1000);

        progressBar.setMax(10000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void donateButtonPressed (View view)
    {
        int amount  = amountPicker.getValue();
        int radioId = paymentMethod.getCheckedRadioButtonId();

        String method = "";
        if (radioId == R.id.PayPal){
            method = "PayPal";
        } else {
            method = "Direct";
        }

        TITLES[0] = "Donated " + totalDonated;
        totalDonated = totalDonated + amount;
        progressBar.setProgress(totalDonated);

        String msg = "Total so far $"+totalDonated;
        totalTextView.setText(msg);

        Log.v("Donate", "Donate Pressed! with amount " + amount + ", method: " + method);
        Log.v("Donate", "Current total " + totalDonated);
    }
    String[] TITLES = new String[3];

    public String[] getDonationsArray() {
        return TITLES.clone();
    }
}

DonationHistory.java

public class DonationHistory extends ListFragment {
    ListView listview;
    View view;

    MainActivity newDonationData = new MainActivity();
    String[] Array = newDonationData.getDonationsArray();

    public View onCreateView(LayoutInflater i,ViewGroup container,Bundle savedInstanceState)
    {

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_1,R.id.text1,Array);

        setListAdapter(adapter);
        return super.onCreateView(i,container,savedInstanceState);
    }
}
Clewy
  • 21
  • 1
  • 1
    Show the logcat and error line. – GHH Nov 10 '20 at 07:07
  • https://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class#:~:text=You%20cannot%20just%20create%20objects,valid%20context%20attached%20to%20them. – Android Developer Nov 10 '20 at 07:27

1 Answers1

1

your Fragment have some big flaws...

MainActivity newDonationData = new MainActivity();

you just can't create Acivity in such way, don't do this never ever! Even if you could - this is new object and its String[] TITLES is always empty (as freshly created)

inside Fragment you may use getActivity() method for getting Activity to which Fragment is attached

((MainActivity) getActivity()).getDonationsArray();

you can use getActivity() only after onActivityCreated, check out Fragment lifecycle in HERE

also you are using setListAdapter before super.onCreateView( call - this may lead to NPE, as ListView isn't inflated/set up and adapter don't have View to attach to. move setListAdapter to onViewCreated method, after super call. BUT you need data straight from Activity, so in your case better approach would be to attach adapter in onActivityCreated (also after super call)

all above is a guess as you didn't provided any logcat with error, I even don't see how do you start your DonationHistory Fragment (fixed in XML?)

snachmsm
  • 17,866
  • 3
  • 32
  • 74