【问题标题】:My activity is FORCE CLOSING as soon as i run.. I have used a ListActivity and created a custom adapter我的活动一运行就强制关闭。我使用了 ListActivity 并创建了一个自定义适配器
【发布时间】:2011-04-09 19:35:03
【问题描述】:

这是我的 .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 我有
线性布局
-> 复选框
-> 按钮
-> 文本视图

我在 main.xml 中做了
线性布局
-> 列表视图
-> 文本视图

我在我的 LOGCAT 中得到了这个:
致命例外:
无法启动活动 COMPONENT.INFO

【问题讨论】:

  • 如果有堆栈跟踪以及 main.xml 的内容,你能发布吗?
  • 请编辑您的问题并发布与您的“强制关闭”相关的整个堆栈跟踪,而不仅仅是您选择包含的一行。
  • 我修好了伙计们...无论如何感谢您的关注..

标签: android listview arraylist listactivity android-arrayadapter


【解决方案1】:

可能会让您感到困惑的一件事是,ListActivity 的 xml 布局必须包含一个带有 android:id="@android:id/list" 的 ListView,但如果没有您的 main.xml 文件和堆栈跟踪,就很难判断这是否是问题所在。

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多