我的活动一运行就强制关闭。我使用了 ListActivity 并创建了一个自定义适配器
这是我的 .java 文件:
public class List1 extends ListActivity {
/** Called when the activity is first created. */
ListView lv1;
private ArrayList<Tree> m_orders;
private TreeAdapter m_adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getItems();
this.m_adapter = new TreeAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);
}
public void getItems()
{
m_orders=new ArrayList<Tree>();
Tree t=new Tree();
t.setItemName("Document");
m_orders.add(t);
t.setItemName("Address Book");
m_orders.add(t);
}
}
class TreeAdapter extends ArrayAdapter<Tree>
{
private ArrayList<Tree> it;
public TreeAdapter(Context context, int textViewResourceId,ArrayList<Tree> items)
{
super(context, textViewResourceId, items);
this.it=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Tree o = it.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
Button btn = (Button)v.findViewById(R.id.theButton);
if (tt != null) {
tt.setText("Name is " + o.getItemName());
}
if(btn!=null){
btn.setTag(o);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Tree o = (Tree)v.getTag();
String message = o.getItemName() + " clicked";
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
return v;
}
}
在 row.xml 中我有
线性布局
->复选框
->按钮
-> TextView
在 main.xml 中我做了
线性布局
->列表视图
-> TextView
我在 LOGCAT 中得到了这个:
致命异常:
无法启动活动组件.INFO
This is my .java file:
public class List1 extends ListActivity {
/** Called when the activity is first created. */
ListView lv1;
private ArrayList<Tree> m_orders;
private TreeAdapter m_adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getItems();
this.m_adapter = new TreeAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);
}
public void getItems()
{
m_orders=new ArrayList<Tree>();
Tree t=new Tree();
t.setItemName("Document");
m_orders.add(t);
t.setItemName("Address Book");
m_orders.add(t);
}
}
class TreeAdapter extends ArrayAdapter<Tree>
{
private ArrayList<Tree> it;
public TreeAdapter(Context context, int textViewResourceId,ArrayList<Tree> items)
{
super(context, textViewResourceId, items);
this.it=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Tree o = it.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
Button btn = (Button)v.findViewById(R.id.theButton);
if (tt != null) {
tt.setText("Name is " + o.getItemName());
}
if(btn!=null){
btn.setTag(o);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Tree o = (Tree)v.getTag();
String message = o.getItemName() + " clicked";
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
return v;
}
}
In row.xml i have
LinearLayout
-> Checkbox
-> Button
-> TextView
In main.xml i did
LinearLayout
-> ListView
-> TextView
I am getting this in my LOGCAT:
FATAL EXCEPTION:
UNABLE TO START ACTIVITY COMPONENT.INFO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能让您困惑的一件事是 ListActivity 的 xml 布局必须包含带有
android:id="@android:id/list"
的 ListView,但很难判断这是否是问题所在没有你的 main.xml 文件和堆栈跟踪。One thing that might be tripping you up is that the xml layout for a ListActivity must include a ListView with
android:id="@android:id/list"
but it's hard to tell if that is the issue without your main.xml file and the stack trace.