Tab下的ListView和MapActivity使用类
我正在尝试将 MapActivity 和 ListActivity 放入选项卡中。我找到了一种方法,但我只是不喜欢它的样子(我宁愿使用一些类来使我的代码更加模块化)
现在,我在我的主要活动中有这段代码
Resources res = getResources(); // Resource object to get Drawables
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, OptionsActivity.class);
spec = tabHost.newTabSpec(OPTS_TAB_TAG).setIndicator("Options",
res.getDrawable(R.drawable.ic_tab_options))
.setContent(intent);
tabHost.addTab(spec);
:类:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class OptionsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] options = getResources().getStringArray(R.array.options_array);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options));
ListView lv = getListView();
lv = (ListView) findViewById(R.id.opt);
lv.setEmptyView((TextView) findViewById(R.id.options));
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, options));
}
}
和我的布局 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<ListView
android:id="@+id/opt"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0fFbO-eNMhj2lDaC6b4YBfpo1DikBMixPYiEJCw" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
但由于某种原因,当我选择此“选项”选项卡时,我的应用程序强制关闭...
有人可以告诉我我在这里做错了什么吗?
I am trying to put a MapActivity and a ListActivity in Tabs. I found a way of doing it, but I just don't like the way it looks like (I rather use some classes to make my code more modular)
Now, I have this code in my main activity:
Resources res = getResources(); // Resource object to get Drawables
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, OptionsActivity.class);
spec = tabHost.newTabSpec(OPTS_TAB_TAG).setIndicator("Options",
res.getDrawable(R.drawable.ic_tab_options))
.setContent(intent);
tabHost.addTab(spec);
And this one in the OptionsActivity class:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class OptionsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] options = getResources().getStringArray(R.array.options_array);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options));
ListView lv = getListView();
lv = (ListView) findViewById(R.id.opt);
lv.setEmptyView((TextView) findViewById(R.id.options));
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, options));
}
}
and my layout main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<ListView
android:id="@+id/opt"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0fFbO-eNMhj2lDaC6b4YBfpo1DikBMixPYiEJCw" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
But for some reason, when I select this "Options" tab, my application has a force close...
Can someone show me what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有使用任何布局,因此从您获取
lv = (ListView) findViewById(R.id.opt);
的地方将代码放入 try catch 块中,如下所示尝试{
ListView lv = getListView();
然后调试一次
you are not using any layout so from where you are getting
lv = (ListView) findViewById(R.id.opt);
put code in try catch block like thistry{
ListView lv = getListView();
then debug it once