'RunTime error in android studios, with no syntax errors. Created program programmatically

I am new to android studios and was tasked with creating an app programmatically. Depending on which button is pressed, the color description is displayed. I have a feeling it has something to do with the layout I created. Every time I run the program the app crashes and I cannot figure out why. Below is my code:

    package com.example.hw3ex2;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;





public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    LinearLayout layout;
    LinearLayout.LayoutParams layoutParams;
    String plumDescription = getResources().getString(R.string.plum_is);
    String blueDescription = getResources().getString(R.string.blue_is);
    String goldDescription = getResources().getString(R.string.gold_is);
    String descriptionString;
    TextView tv;
    Button btnPlum;
    Button btnBlue;
    Button btnGold;
    private int button_color = getResources().getColor(R.color.button_color);
    private int background_color=getResources().getColor(R.color.background_color);



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

    private void buildGUIByCode() {

        LayoutParams params =
                new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);

        //create a layout
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);


        //create textview
        tv = new TextView(this);
        tv.setLayoutParams(params);
        layout.addView(tv);


        //create buttons and add them to layout
        btnPlum = new Button(this);
        btnPlum.setTag("plum");
        btnPlum.setText("Plum");
        btnPlum.setLayoutParams(params);
        layout.addView(btnPlum);
        btnPlum.setOnClickListener(this);
        btnPlum.setBackgroundColor(button_color);


        btnBlue = new Button(this);
        btnBlue.setTag("blue");
        btnBlue.setText("Blue");
        btnBlue.setLayoutParams(params);
        layout.addView(btnBlue);
        btnBlue.setOnClickListener(this);
        btnBlue.setBackgroundColor(button_color);

        btnGold = new Button(this);
        btnGold.setTag("gold");
        btnGold.setText("Gold");
        btnGold.setLayoutParams(params);
        layout.addView(btnGold);
        btnGold.setOnClickListener(this);
        btnGold.setBackgroundColor(button_color);



        setContentView(layout);

    }
    @Override
    public void onClick(View view) {
        Object tag = view.getTag();
        if ("plum".equals(tag)) {//get plum_is string and display in textView
            tv.setText(plumDescription);
        } else if ("blue".equals(tag)) {//get blue_is string and display in textview
            tv.setText(blueDescription);
        } else if ("gold".equals(tag)) {//get gold_is string and display in textview
            tv.setText(goldDescription);
        }
        System.out.println(descriptionString);

    }

}


Solution 1:[1]

I see that you're invoking getResources() before onCreate, you need to move all of those initialization that uses getResources() within onCreate

public class MainActivity ... {

    String plumDescription; // don't initialize here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        plumDescription = getResources().getString(R.string.plum_is); // initialize after onCreate
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praveen G