Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中将按钮动态添加到LinearLayout时获取空指针异常_Android_Nullpointerexception_Android Linearlayout_Android Button - Fatal编程技术网

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

在android中将按钮动态添加到LinearLayout时获取空指针异常,android,nullpointerexception,android-linearlayout,android-button,Android,Nullpointerexception,Android Linearlayout,Android Button,我创建了一个android应用程序,用于动态创建多维按钮,以LinearLayout,行数取自数据库,其中列数固定为5 问题是当我把行数设为11时,一切都正常……但是当我把行数设为12时,我得到的是null指针异常 rowLayout.addView(buttons[i][j],param); 有谁能告诉我为什么会发生这种情况以及解决办法吗 public void createButtons() { LinearLayout layoutVertical = (Li

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

问题是当我把行数设为11时,一切都正常……但是当我把行数设为12时,我得到的是null指针异常

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

直到您的行数达到11,屏幕上的视图才可见。当您添加一个在屏幕上看不到的行时,您会得到空值

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


希望这有帮助

您能告诉我一些解决方案吗?您必须在滚动视图时使用回调函数(可能是getView())。在那里动态添加行并返回该视图。另外,继续删除未使用的视图。顺便问一下,为什么不从适配器向rowlayout提供数据呢。这应该是正确的方法。我刚刚尝试过谷歌搜索,发现这个链接可能对你有用:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotouch/com.dinein.Dinein_Tables}: java.lang.NullPointerException