我正在创建TableLayout
并以编程方式向其添加视图。当我添加TextView
和图片时,它正常工作。当我尝试使用RadioGroup
添加RadioButtons
时它显示错误。
在我的Xml布局中,我在滚动视图下面有tablelayout,下面是我的Java代码:
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;
public class DamnitActivity extends Activity {
TableLayout makeit;
ArrayList list_name;
int color_blue = -16776961;
int color_gray = -7829368;
int color_black = -16777216;
int color_white = -1;
final int CHECK_BUTTON_ID = 982301;
int ids_check[];
boolean bool_check[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
makeit = (TableLayout) findViewById(R.id.tableLayout1);
list_name = new ArrayList();
list_name.add("Close");
list_name.add("Cristiano");
list_name.add("David");
list_name.add("Fernando");
list_name.add("Messi");
list_name.add("Kaka");
list_name.add("Wayne");
list_name.add("ghse");
list_name.add("gouse");
list_name.add("ghuse");
list_name.add("ghous");
list_name.add("ouse");
list_name.add("use");
list_name.add("e");
list_name.add("eff");
list_name.add("euyr");
list_name.add("ejjyytuty");
list_name.add("madre");
list_name.add("yuir");
list_name.add("eyrty");
list_name.add("etytr");
list_name.add("ewrrtt");
bool_check = new boolean[list_name.size()];
ids_check = new int[list_name.size()];
createTableRows();
}
public void onListItemClick(ListView parent, View v, int position, long id){
}
public void createTableRows()
{
for (int i = 0; i < list_name.size(); i++)
{
TextView tv_name = new TextView(this);
Button btn_check = new Button(this);
ImageView img_line = new ImageView(this);
RadioGroup mgroup=new RadioGroup(this);
RadioButton newRadioButton = new RadioButton(this);
newRadioButton.setText("hi");
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
mgroup.addView(newRadioButton, 0, layoutParams);
tv_name.setText((CharSequence) list_name.get(i));
tv_name.setTextColor(color_blue);
tv_name.setTextSize(30);
tv_name.setTypeface(Typeface.DEFAULT_BOLD);
tv_name.setWidth(150);
btn_check.setLayoutParams(new LayoutParams(30, 30));
btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked);
img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
img_line.setBackgroundResource(R.drawable.separater_line);
makeit.addView(tv_name);
makeit.addView(btn_check);
makeit.addView(mgroup);
int id = i + CHECK_BUTTON_ID;
btn_check.setId(id);
ids_check[i] = id;
btn_check.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
for (int j = 0; j < ids_check.length; j++)
{
Button btn_check_1 = (Button) findViewById(ids_check[j]);
if(v.getId() == ids_check[j])
if(bool_check[j])
{
btn_check_1.setBackgroundResource(R.drawable.small_checkbox_unchecked);
bool_check[j] = false;
}
else
{
btn_check_1.setBackgroundResource(R.drawable.small_checkbox_checked);
bool_check[j] = true;
}
}
}
});
}
}
}
错误是:
2-10 11:40:24.120:E / AndroidRuntime(995):java.lang.RuntimeException:无法启动活动ComponentInfo {com.pace.damnit / com.pace.damnit.DamnitActivity}:java.lang。的NullPointerException
答案 0 :(得分:0)
很可能 makeit TableLayout对象为空