我正在尝试在标签中实现列表视图。我正在使用自定义arrayadapter来创建自定义列表视图。代码如下。
创建标签活动:
TabHost th = getTabHost();
TabSpec specGroups = th.newTabSpec("Groups");
specGroups.setIndicator("Groups");
Intent intentGroups = new Intent(this, GroupsList.class);
specGroups.setContent(intentGroups);
群组列表活动:
public class GroupsList extends Activity {
public String[] ROSTER_LIST = {"Sam", "Bob", "Tabg", "Toushi", "john"};
private ListView ROSTER_LISTVIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.groups_list);
Log.d("STARTED", "0");
ROSTER_LISTVIEW = (ListView) findViewById(R.id.listViewFriends);
Log.d("STARTED", "1");
ArrayAdapter<String> adapter = new MyAdapter(this,
android.R.layout.simple_list_item_1, R.id.textViewRosterRow,
ROSTER_LIST);
Log.d("STARTED", "2");
ROSTER_LISTVIEW.setAdapter(adapter);
Log.d("STARTED", "3");
}
自定义适配器类(内部类):
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] ROSTER_LIST) {
super(context, resource, textViewResourceId, ROSTER_LIST);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ADAPTER", "0");
View row = inflater
.inflate(R.layout.friends_list_row, parent, false);
Log.d("ADAPTER", "1");
TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
Log.d("ADAPTER", "2");
text.setText(ROSTER_LIST[position]);
Log.d("ADAPTER", "OK");
return row;
}
}
在oncreate方法Log.d("STARTED", "2");
行内登录logcat,然后弹出nullpointerexception。 no Log
登录MyAdapter
内部类中的logcat。
此代码在没有标签的情况下执行正常。
我在这里犯了什么错误?我怎么解决这个问题?提前谢谢:)
答案 0 :(得分:0)
将适配器getView更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ADAPTER", "0");
row = inflater
.inflate(R.layout.friends_list_row, parent, false);
Log.d("ADAPTER", "1");
}
TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
Log.d("ADAPTER", "2");
text.setText(ROSTER_LIST[position]);
Log.d("ADAPTER", "OK");
return row;
}