Android:在选项卡布局中使用 ListActivity 时出错
如果这是一个重复的问题,我提前表示歉意,我查遍了所有内容,但找不到任何解决方案来帮助我。
我已按照 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
ListActivity
时,ListView
的 ID 必须是@android:id/list
the ID of your
ListView
must be@android:id/list
when using aListActivity
您是否在清单中声明了新活动?如果您尝试为未在那里声明的活动创建选项卡,它将崩溃。
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.