Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Java 通过字符串数组以编程方式填充Tablelayout_Java_Android - Fatal编程技术网

Java 通过字符串数组以编程方式填充Tablelayout

Java 通过字符串数组以编程方式填充Tablelayout,java,android,Java,Android,大家好,各位飞越者 我写这段代码只是为了在我的activity_客户布局中填充一个tablelayout。出于某种原因,在中运行时没有抛出任何错误,但在“populateView”结束时,应用程序只是停止/崩溃 我尝试了很多通过google/stackoverflow找到的东西,但似乎都不管用。我希望有人能帮我找到应用程序停止的原因 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance

大家好,各位飞越者

我写这段代码只是为了在我的activity_客户布局中填充一个tablelayout。出于某种原因,在中运行时没有抛出任何错误,但在“populateView”结束时,应用程序只是停止/崩溃

我尝试了很多通过google/stackoverflow找到的东西,但似乎都不管用。我希望有人能帮我找到应用程序停止的原因

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer);
    populateView(DataClass.getReturnData());
}

private void populateView(String[] Array){
    int len = Array.length;

    TableLayout tab = (TableLayout) findViewById(R.id.table);
    if (len != 0){
        for (int i = 0; i <= len - 1; i++) {
            TableRow row = new TableRow(this);                
            TextView tvName = new TextView(this);
            tvName.setText("" + Array[i]);
            System.out.println(Array[i]);
            tab.addView(row);
        }
    }
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
populateView(DataClass.getReturnData());
}
私有void populateView(字符串[]数组){
int len=Array.length;
TableLayout选项卡=(TableLayout)findViewById(R.id.table);
如果(len!=0){

对于(int i=0;i,这里有一个完整的示例:

 public class MainActivity extends Activity
{


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

        String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
                         "Row 7"
                       };
        String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
                            "COLUMN5", "COLUMN6"
                          };
        int rl=row.length;
        int cl=column.length;

        ScrollView sv = new ScrollView(this);
        TableLayout tableLayout = createTableLayout(row, column,rl, cl);
        HorizontalScrollView hsv = new HorizontalScrollView(this);

        hsv.addView(tableLayout);
        sv.addView(hsv);
        setContentView(sv);

    }

    private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount)
    {
        // 1) Create a tableLayout and its params
        TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setBackgroundColor(Color.BLACK);

        // 2) create tableRow params
        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
        tableRowParams.setMargins(1, 1, 1, 1);
        tableRowParams.weight = 1;

        for (int i = 0; i < rowCount; i++)
        {
            // 3) create tableRow
            TableRow tableRow = new TableRow(this);
            tableRow.setBackgroundColor(Color.BLACK);

            for (int j= 0; j < columnCount; j++)
            {
                // 4) create textView
                TextView textView = new TextView(this);
                //  textView.setText(String.valueOf(j));
                textView.setBackgroundColor(Color.WHITE);
                textView.setGravity(Gravity.CENTER);

                String s1 = Integer.toString(i);
                String s2 = Integer.toString(j);
                String s3 = s1 + s2;
                int id = Integer.parseInt(s3);
                Log.d("TAG", "-___>"+id);
                if (i ==0 && j==0)
                {
                    textView.setText("0==0");
                }
                else if(i==0)
                {
                    Log.d("TAAG", "set Column Headers");
                    textView.setText(cv[j-1]);
                }
                else if( j==0)
                {
                    Log.d("TAAG", "Set Row Headers");
                    textView.setText(rv[i-1]);
                }
                else
                {
                    textView.setText(""+id);
                    // check id=23
                    if(id==23)
                    {
                        textView.setText("ID=23");

                    }
                }

                // 5) add textView to tableRow
                tableRow.addView(textView, tableRowParams);
            }

            // 6) add tableRow to tableLayout
            tableLayout.addView(tableRow, tableLayoutParams);
        }

        return tableLayout;
    }
}
公共类MainActivity扩展活动
{
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
字符串[]行={“第1行”、“第2行”、“第3行”、“第4行”、“第5行”、“第6行”,
“第7行”
};
String[]column={“COLUMN1”、“COLUMN2”、“COLUMN3”、“COLUMN4”,
“第5栏”、“第6栏”
};
int rl=行长度;
int cl=列长度;
ScrollView sv=新的ScrollView(此);
TableLayout TableLayout=createTableLayout(行、列、rl、cl);
HorizontalScrollView hsv=新的HorizontalScrollView(此);
hsv.addView(表格布局);
sv.addView(hsv);
setContentView(sv);
}
私有TableLayout createTableLayout(字符串[]rv、字符串[]cv、整数行计数、整数列计数)
{
//1)创建tableLayout及其参数
TableLayout.LayoutParams tableLayoutParams=新建TableLayout.LayoutParams();
TableLayout TableLayout=新的TableLayout(本);
tableLayout.setBackgroundColor(颜色:黑色);
//2)创建tableRow参数
TableRow.LayoutParams tableRowParams=新建TableRow.LayoutParams();
tableRowParams.setMargins(1,1,1,1);
tableRowParams.weight=1;
对于(int i=0;i”+id);
如果(i==0&&j==0)
{
setText(“0==0”);
}
else如果(i==0)
{
Log.d(“TAAG”,“设置列标题”);
textView.setText(cv[j-1]);
}
else如果(j==0)
{
Log.d(“TAAG”,“设置行标题”);
textView.setText(rv[i-1]);
}
其他的
{
textView.setText(“+id”);
//检查id=23
如果(id==23)
{
setText(“ID=23”);
}
}
//5)将textView添加到tableRow
tableRow.addView(textView,tableRowParams);
}
//6)将tableRow添加到tableLayout
tableLayout.addView(tableRow,tableLayoutParams);
}
返回表布局;
}
}
输出:


提供日志详细信息。此说明向我展示了使其工作所需的一切。