简体   繁体   中英

Why does my linearLayout behave like it is null

When I try to add the button created in an object to the linearLayout, it behaves as if the button is null, even though it has been initialized.

I have tested that the button isnt actually null and that the linear layout is declared correctly, which it is.

The linearLayout goes from being not null in onCreate() to null in getLinearLayout();

   public Province(Context context, int id, int x, int y){
        this.id = id;
        this.x = x;
        this.y = y;
        button = new Button(context); //creates button, context is passed in through the MainActivity
        selected = false;
        selection(); //selection call
    }
//selection method
    public Button selection(){
        linearLayout = getLinearLayout();  //layout from MainActivity
        button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(button);  //adds button to current view, button is not null despite error
        button.setId(id);
        button.setX(x);
        button.setY(y);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(getCurrentPlayer().getStage() == 0) {
                    troops++;
                    getCurrentPlayer().modTroops(-1);
                }
                else {
                    if (selected)
                        selected = false;
                    else if (!selected && getCurrentPlayer().select(0) > 0)
                        selected = true;
                }
            }
        });
        return button;
    }

Here is the code where linearLayout is defined;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = findViewById(R.id.linear);  //refrences MainActivity xml with id linear
        //linearLayout is not null here;
        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        map = new Map(this, 0);
    }

Here is the get method:

public LinearLayout getLinearLayout(){
        if(linearLayout == null)
            throw new IllegalArgumentException("Linear Layout is null");
        //the exeption is thrown here so linearLayout is now null;
        return linearLayout;
    }

This is the error that appears:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
        at com.example.hedgemonics.Province.selection(Province.java:56)
        at com.example.hedgemonics.Province.<init>(Province.java:28)
        at com.example.hedgemonics.Map.<init>(Map.java:10)
        at com.example.hedgemonics.MainActivity.onCreate(MainActivity.java:28)

Try replacing the context in Button declaration with the this keyword as follows:

button = new Button(this); 

instead of:

button = new Button(context);

And also try making the Button global, if the above solution didn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM