Android 按钮数组

发布于 2024-12-25 14:08:35 字数 455 浏览 4 评论 0原文

无法找到在 android 中制作按钮数组的方法。

这是我尝试过的代码,但我收到了 java.lang.NullPointerException。

    private Button[] button = {(Button) findViewById(R.id.cGuess1),
        (Button) findViewById(R.id.cGuess2),(Button)
        findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};

这可能吗?

编辑:

对不起大家。刚刚意识到我的错误!

我试图为整个类声明数组,并尝试在 onCreate 之前从 ids 获取视图,因此没有 setContentView(R.layout.game);

对不起。

Can't work out a way to make an array of buttons in android.

This is the code I have tried but I'm getting a java.lang.NullPointerException.

    private Button[] button = {(Button) findViewById(R.id.cGuess1),
        (Button) findViewById(R.id.cGuess2),(Button)
        findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};

Is this even possible?

EDIT:

Sorry everyone. Just realised my mistake!

I was trying to declare the array for my whole class and trying to get the views from the ids before onCreate so there was no setContentView(R.layout.game);

Sorry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2025-01-01 14:08:35

由于没有其他人发布解决方案的实际代码,因此这里有一个工作片段。

Button[] myButtons = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myButtons = new Button[]
    {
            (Button)findViewById(R.id.button1),
            (Button)findViewById(R.id.button2),
            (Button)findViewById(R.id.button3),
    };
}

Since no one else posted actual code for a solution, here's a working snippet.

Button[] myButtons = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myButtons = new Button[]
    {
            (Button)findViewById(R.id.button1),
            (Button)findViewById(R.id.button2),
            (Button)findViewById(R.id.button3),
    };
}
如梦 2025-01-01 14:08:35

只是猜测,因为这里没有完整的代码,您是否在创建数组之前调用了 setContentView() 。

Just a guess as full code is not available here, have you called setContentView() before creating array.

只是偏爱你 2025-01-01 14:08:35

你能尝试一下吗

final Button[] button = {(Button) findViewById(R.id.cGuess1), 
    (Button) findViewById(R.id.cGuess2),(Button) 
    findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};

Could you try

final Button[] button = {(Button) findViewById(R.id.cGuess1), 
    (Button) findViewById(R.id.cGuess2),(Button) 
    findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};
夜清冷一曲。 2025-01-01 14:08:35

您的按钮之一可能为空。并且放置 private 关键字不允许我创建数组。另请注意,首先您要为您的活动设置 cententView,然后访问这些按钮。

One of your buttons may be null. And putting a private keyword does not allow me to create the array. Also see that Firstly you are setting the cententView for your activity and then accessing these buttons.

瀞厅☆埖开 2025-01-01 14:08:35
public class main2 extends Activity{
    final int[] button = {R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
            R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.button10};
    Button[] bt = new Button[button.length];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign);
        for(int i=0;i<button.length;i++){
            final Context context = this;
            final int b = i;
            bt[b]= (Button) findViewById(button[b]);
            Typeface font = Typeface.createFromAsset(getAssets(), "Angkor.ttf");
            bt[b].setTypeface(font);
            bt[b].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(context,r1.class);
                    startActivity(myIntent);
                }
            });
        }
    }
}
public class main2 extends Activity{
    final int[] button = {R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
            R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.button10};
    Button[] bt = new Button[button.length];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign);
        for(int i=0;i<button.length;i++){
            final Context context = this;
            final int b = i;
            bt[b]= (Button) findViewById(button[b]);
            Typeface font = Typeface.createFromAsset(getAssets(), "Angkor.ttf");
            bt[b].setTypeface(font);
            bt[b].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(context,r1.class);
                    startActivity(myIntent);
                }
            });
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文