java - 为什么我的 LinearLayout 表现得像 null

标签 java android

当我尝试将在对象中创建的按钮添加到 LinearLayout 时,它的行为就好像该按钮为 null,即使它已被初始化。

我已经测试过该按钮实际上并非为空,并且线性布局已正确声明,事实确实如此。

线性布局从 onCreate() 中的非 null 变为 getLinearLayout() 中的 null;

   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;
    }

这是定义 LinearLayout 的代码;

@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);
    }

这是获取方法:

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;
    }

这是出现的错误:

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)

最佳答案

尝试用 this 关键字替换 Button 声明中的上下文,如下所示:

button = new Button(this); 

而不是:

button = new Button(context);

如果上述解决方案不起作用,还可以尝试将按钮设置为全局。

关于java - 为什么我的 LinearLayout 表现得像 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343129/

相关文章:

android - 如何在所有移动浏览器上正确显示网页

android - 来自 C 的 dlopen C++ lib,然后将 std::string 与函数一起使用

java - 这段 Java 1.7 代码片段中的 TLS 版本是什么?

java - Tomcat HttpServlet 的 ClassNotFoundException

java - 运行 Java 代码时内存不足

Java - Thread 类型的 sleep(int) 方法未定义

java - 在 Activity 之间传输数据

android - PhoneGap 忽略设置了特定宽度的视口(viewport)标签?

AndroidX Espresso 测试 : No tests were found and Empty test suite

java - 在 JFileChooser 中隐藏 "choosable file filter"小部件