错误:java.lang.NullPointerexception:尝试调用虚方法void android.widget.TableLayout.addView

时间:2017-06-07 07:06:56

标签: android android-tablelayout

我收到了这个NULL POINTER EXCEPTION ERROR。需要解决这个问题。

尝试在Table中显示数据,在日志中数据似乎被提取,但它不以表格格式显示。

try {
                        jsonArray = new JSONArray(respString);
                        Log.d("jsonArray",jsonArray.toString());
                        TableRow[] tr_head = new TableRow[jsonArray.length()];
                        JSONObject jobj = null;
                        for(int i=0;i<jsonArray.length();i++){
                            jobj = jsonArray.getJSONObject(i);
                            tr_head[i] = new TableRow(getApplicationContext());
                            tr_head[i].setBackgroundColor(Color.GRAY);
                            tr_head[i].setLayoutParams(new TableRow.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
                            TextView tv_item1 = new TextView(getApplicationContext());
                            TextView tv_item2 = new TextView(getApplicationContext());
                            TextView tv_item3 = new TextView(getApplicationContext());
                            TextView tv_item4 = new TextView(getApplicationContext());
                            Log.d("Table Name",jobj.getString("tb_name"));
                            tv_item1.setText(jobj.getString("tb_name"));
                            tv_item1.setGravity(Gravity.CENTER);
                            tv_item2.setText(jobj.getString("cust_name"));
                            tv_item2.setGravity(Gravity.CENTER);
                            tv_item3.setText(jobj.getString("ord_date"));
                            tv_item3.setGravity(Gravity.CENTER);
                            tv_item4.setText(jobj.getString("ord_time"));
                            tv_item4.setGravity(Gravity.CENTER);
                            tr_head[i].addView(tv_item1);
                            tr_head[i].addView(tv_item2);
                            tr_head[i].addView(tv_item3);
                            tr_head[i].addView(tv_item4);
                            t1.addView(tr_head[i], new TableLayout.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));

                        }

0 个答案:

没有答案