我的活动一运行就强制关闭。我使用了 ListActivity 并创建了一个自定义适配器

发布于 2024-10-31 09:00:17 字数 2497 浏览 4 评论 0原文

这是我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-11-07 09:00:17

可能让您困惑的一件事是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文