Android:在选项卡布局中使用 ListActivity 时出错

发布于 2024-09-27 08:14:47 字数 1505 浏览 4 评论 0原文

如果这是一个重复的问题,我提前表示歉意,我查遍了所有内容,但找不到任何解决方案来帮助我。

我已按照 android 开发教程 创建一个选项卡式 UI为每个选项卡使用单独的活动。

我让它工作得很好。直到...

我尝试将 ListView 放入选项卡式活动之一 (Tab1) 中。为了获得我想要的可用性,我发现我需要扩展 ListActivity。那是我收到“强制关闭”错误的时候。当我延长常规活动时,它显示得很好。

这是我的非功能性 Tab1.java 代码:

public class Tab1 extends ListActivity {
    ListView lv;
    String[] times = {
        "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM",
            "12:00 AM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM",
            "5:00 PM", "6:00 PM", "7:00 PM"
    };

    /** Called when the activity is first created. */@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);

        lv = (ListView) findViewById(R.id.ListView_Tab1);
        lv.setAdapter(new ArrayAdapter < String > (this, R.layout.list_item, R.id.times,
            times));
        lv.setOnItemClickListener(new OnItemClickListener() {@
            Override
            public void onItemClick(AdapterView <? > parent, View view,
                int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                    Toast.LENGTH_SHORT).show();
            }
        });
    }
}

My apologies in advance if this is a repeat question, I looked all over and couldn't find any solution to help me.

I have followed the android dev tutorial for creating a tabbed UI that uses a separate activity for each tab.

And I got it working just fine. Until...

I am attempting to put a ListView inside one of the tabbed activities (Tab1). To get the usability I want, I find that I need to extend ListActivity. Thats when I get the 'Force close' error. It displays just fine when I extend regular Activity.

Here is my nonfunctional Tab1.java code:

public class Tab1 extends ListActivity {
    ListView lv;
    String[] times = {
        "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM",
            "12:00 AM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM",
            "5:00 PM", "6:00 PM", "7:00 PM"
    };

    /** Called when the activity is first created. */@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);

        lv = (ListView) findViewById(R.id.ListView_Tab1);
        lv.setAdapter(new ArrayAdapter < String > (this, R.layout.list_item, R.id.times,
            times));
        lv.setOnItemClickListener(new OnItemClickListener() {@
            Override
            public void onItemClick(AdapterView <? > parent, View view,
                int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                    Toast.LENGTH_SHORT).show();
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

绝對不後悔。 2024-10-04 08:14:47

使用 ListActivity 时,ListView 的 ID 必须是 @android:id/list

the ID of your ListView must be @android:id/list when using a ListActivity

非要怀念 2024-10-04 08:14:47

您是否在清单中声明了新活动?如果您尝试为未在那里声明的活动创建选项卡,它将崩溃。

Did you declare the new Activity in your manifest? If you try and create a tab for an Activity not declare there, it will crash.

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