【问题标题】:Create a button and add it to a view programmatically创建一个按钮并以编程方式将其添加到视图中
【发布时间】:2012-04-10 12:07:45
【问题描述】:

我正在为 Android 开发流行的扫雷游戏版本。我正在尝试以编程方式创建一个按钮并将其添加到RelativeLayout。我在这里发现了一些非常相似的东西:How do I programmatically add buttons into layout one by one in several lines?

当我尝试运行它时,我在以下位置收到 NullPointerException:

RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);

这是整个代码块:

public void create() {
    RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
    for(int i = 0; i < gridSize; i++) {
        if(grid[i] == 0) { //if grid pos. indicates an empty cell
            Button empty = new Button(this);
            empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
            empty.setId(i); //set id to value of i
            empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            layout1.addView(empty); //add the button to the relativeLayout view
            //((Button) findViewById(i)).setOnClickListener(emptyListener); 
        }

提前感谢您的任何回复

【问题讨论】:

  • 我认为您的问题无法到达您的 game.xml 。你能提供你的结构(大纲)吗?
  • 我正在使用整数数组来模拟扫雷字段。例如,位置 [2] 处的值 9 表示扫雷区域的位置 2 处有地雷。然后我使用 if 语句生成不同的按钮,即如果 position[2] == 9 将创建一个代表地雷的按钮。我正在尝试将这些按钮添加到代表扫雷字段的相对布局中。这有帮助吗?

标签: java android android-layout


【解决方案1】:

已经通过setContentView(R.layout.xxxx)设置了Activity的布局xml?

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);


...

这个

 RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);

应该是

 RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.relative_id);

R.id...用于映射控件,RelativeLayout是一个控件。

【讨论】:

  • 是的,我已经设置了内容视图,我更改了对 ID 的引用而不是文件名本身,但是当活动开始时我仍然看不到任何按钮。我在 onCreate() 方法中调用了 create() 方法,是否在活动开始时调用了 onCreate() 方法中的任何调用?
  • 不再获得空指针异常。如果我调用 create() 方法,屏幕将保持空白,没有按钮或任何东西
【解决方案2】:

我认为你得到了一个空白屏幕,因为你没有设置内容视图。 我的意思是代码做了它应该做的事情,但是你应该删除顶部的“setContentView()”方法并将它放在最后,然后你应该在关闭 onCreate() 之前将它设置为RelativeLayout方法!像这样的:

public void create() {
RelativeLayout layout1 = new RelativeLayout(this);
for(int i = 0; i < gridSize; i++) {
    if(grid[i] == 0) { //if grid pos. indicates an empty cell
        Button empty = new Button(this);
        empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
        empty.setId(i); //set id to value of i
        empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout1.addView(empty); //add the button to the relativeLayout view
        //((Button) findViewById(i)).setOnClickListener(emptyListener); 
     }
    }
     setContentView(layout1);
   }

另外请注意,我稍微更改了 Relativelayout 的声明。 我希望这有帮助。 :) !

【讨论】:

  • 感谢您的回复,我已更改为表格布局并添加了一些额外的代码,它现在正在工作
【解决方案3】:

您必须输入RelativeLayout 的ID,而不是xml 文件名。 尝试 RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多