我是android编程的新手。 我正在写一个有两个标签的android程序: 用户填写表单并点击保存按钮的detailstab。 listtab,其中显示用户在edittexts控件的详细信息选项卡中插入的内容。 实现这个我做了这个类:
详细java类:
import android.widget.RadioButton;
import android.widget.TextView;
public class detail {
public TextView tvName;
public TextView tvAdr;
public TextView tvNote;
public RadioButton rb_0;
public RadioButton rb_1;
public RadioButton rb_2;
public int radioValue;
//public final TextView tvRadioValue=null;
}
详细说明了我制作一个arraylist并从细节中添加对象的活动类:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class details extends Activity {
public ArrayList<detail> detaillist=new ArrayList<detail>();
public EditText etName;
public EditText etAdr;
public EditText etNote;
public RadioGroup rg;
public RadioButton rb_0;
public RadioButton rb_1;
public RadioButton rb_2;
static details detailsActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detailsActivity=this;
setContentView(R.layout.details_tab);
Button btnSave=(Button) findViewById(R.id.button1);
//define an arraylist to add detail object to it
etName=(EditText) findViewById(R.id.editText1);
etAdr=(EditText) findViewById(R.id.editText2);
etNote=(EditText) findViewById(R.id.editText3);
rg=(RadioGroup) findViewById(R.id.radioGroup1);
rb_0=(RadioButton) findViewById(R.id.radio0);
rb_1=(RadioButton) findViewById(R.id.radio1);
rb_2=(RadioButton) findViewById(R.id.radio2);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final detail d=new detail();
makeDetailList(d);
//clear textboxes
clear(etName);
clear(etAdr);
clear(etNote);
rg.check(R.id.radio0);
}
});
}
public void clear(View v) {
EditText edittext=new EditText(null);
edittext.setText("");
}
public ArrayList<detail> makeDetailList(detail d){
d.tvName.setText(etName.getText());
d.tvAdr.setText(etAdr.getText());
//d.tvNote.setText(etNote.getText());
if(rb_0.isSelected())
{
d.radioValue=0;
//d.tvRadioValue.setText(rb_0.getText());
}
else if (rb_1.isSelected())
{
d.radioValue=1;
//d.tvRadioValue.setText(rb_1.getText());
}
else if (rb_1.isSelected())
{
d.radioValue=2;
//d.tvRadioValue.setText(rb_2.getText());
}
detaillist.add(d);
return detaillist;
}
public static details getInstance(){
return detailsActivity;
}
}
最后列出了活动类 此类包含一个列表视图,用于显示详细信息选项卡中的数据:
public class list extends Activity {
//details details=new details();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_tab);
ListView lv=(ListView) findViewById(R.id.listView1);
//details details=new details();
//ArrayList mylist=details.getInstance().detaillist;
MyAdapter adapter=new MyAdapter(this,android.R.layout.simple_list_item_1 ,
R.id.textView1,
details.getInstance().detaillist);
lv.setAdapter(adapter);
// ArrayAdapter<details> adapter=new ArrayAdapter<details>(this,R.layout.my_list,R.id.textView1,R.id.textView2,));
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
ArrayList<detail> detaillist) {
super(context, resource, textViewResourceId);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.my_list, parent,false);
ArrayList<detail> items=details.getInstance().detaillist;
ImageView ivR=(ImageView) row.findViewById(R.id.imageView1);
TextView tvN=(TextView) row.findViewById(R.id.textView1);
TextView tvA=(TextView) row.findViewById(R.id.textView2);
tvN.setText(items.get(position).tvName.getText());
tvA.setText(items.get(position).tvAdr.getText());
if(items.get(position) != null)
{
if(items.get(position).radioValue==0)
{
ivR.setImageResource(R.drawable.t);
}
else if(items.get(position).radioValue==1)
{
ivR.setImageResource(R.drawable.t);
}
else if(items.get(position).radioValue==2)
{
ivR.setImageResource(R.drawable.t);
}
}
return row;
}
}
}
我收到了一个NullPointException错误onmakeDetailList(d)方法。 问题出在哪儿? 我非常感谢你的帮助 最好的问候
答案 0 :(得分:1)
您尚未初始化详细信息calss中的变量..
d.tvName.setText(etName.getText());
d.tvAdr.setText(etAdr.getText());
在上面的行tvName,tvAdr
中没有初始化..初始化它然后设置文本。
答案 1 :(得分:1)
d.tvName.setText(etName.getText());
您可能没有将tvName设置为对象
答案 2 :(得分:0)
if ( d.tvName == null ) d.tvName = new TextView(this);
if ( d.tvAdr == null ) d.tvAdr = new TextView(this);
之后你可以设置如下文字:
d.tvName.setText(etName.getText());
d.tvAdr.setText(etAdr.getText());