-1

我创建了一个用于动态创建多维按钮的android应用程序LinearLayout,行数取自数据库,其中列数固定为5。

问题是当我将行数设为 11 时,一切正常......但是当我将行数设为 12 时,我在

rowLayout.addView(buttons[i][j],param);

谁能告诉我为什么会发生这种情况以及解决方案

public void createButtons()
    {  
        LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT, 1.0f);
        layoutVertical.setOrientation(LinearLayout.VERTICAL);
        param.setMargins(10, 10, 10, 10);
        LinearLayout rowLayout=null;

        Cursor cursor=db.getPubTables();

        int rowcount =cursor.getCount(); // if rowcount is 11 its working other than that it give null pointer exception eg..if 12
        rowcount--;
        System.out.println("COUNTTTTTTTTTTTTTTT--->"+rowcount);
        Button[][] buttons = new Button[rowcount][5]; 
        int count=rowcount;

        for (int i=0; rowcount>0; i++) 
        {

            if(count%5==1)
            {
                rowLayout = new LinearLayout(this);
                //rowLayout.setBackgroundColor(Color.BLACK);
                rowLayout.setOrientation(LinearLayout.HORIZONTAL);
                //rowLayout.setWeightSum(5);
                layoutVertical.addView(rowLayout,param);
                count=count-5;

            }
            for(int j=0;j<5&&rowcount>0;j++)
            {

                cursor.moveToNext();
                buttons[i][j]=new Button(this);
                buttons[i][j].setText(""+cursor.getString(0));

                System.out.println("SEEEEEEEEEEEEEEEEEEEEEEEEEE:"+cursor.getString(0));
                buttons[i][j].setHeight(55);
                buttons[i][j].setWidth(80);
                buttons[i][j].setTypeface(null, Typeface.BOLD);
                buttons[i][j].setTextColor(Color.BLACK);
                buttons[i][j].setBackgroundResource(R.drawable.dinein_btn_green);

                System.out.println("TABLE STATUS-->"+cursor.getString(1));
                if(cursor.getString(1).equals("reserved"))
                {
                    buttons[i][j].setBackgroundResource(R.drawable.dinein_btn_red);
                }
                if(cursor.getString(1).equals("availabe"))
                {
                    buttons[i][j].setBackgroundResource(R.drawable.dinein_btn_green);
                }
                if(cursor.getString(1).equals("occupied"))
                {
                    buttons[i][j].setBackgroundResource(R.drawable.dinein_btn_red);
                }

                buttons[i][j].setId(Integer.parseInt(cursor.getString(0)));
                rowLayout.addView(buttons[i][j],param); // if row count is 12 here getting nullpointer exception, if row count is 11 its working perfectly
                rowcount--;
            }
        }
        cursor.close();
        db.close();
    }

LOGCAT 输出

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotouch/com.dinein.Dinein_Tables}: java.lang.NullPointerException
4

1 回答 1

0

直到您的行数为 11,该视图对屏幕可见。当您再添加一行在屏幕上不可见时,您将获得空值。

这就是 listView 在内部的工作方式。一次它有 11 行(可以在屏幕上看到)。在滚动时动态删除行并添加新行。

希望这可以帮助

于 2013-08-23T05:40:56.827 回答