在向LinearLayout动态添加按钮时,在android中获取空指针异常

时间:2013-08-23 05:34:54

标签: android nullpointerexception android-linearlayout android-button

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

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

处获得了nullpointer异常
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 OUTPUT

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

1 个答案:

答案 0 :(得分:0)

直到你的行数11,视图对于屏幕是可见的。当您再添加一行在屏幕上无法显示的行时,您将获得空值。

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

希望这有帮助