以下是异常信息:
java.lang.NullPointerException
at android.widget.TextView.setTextColor(TextView.java:1787)
at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:521)
at android.widget.TabHost.addTab(TabHost.java:204)
at com.android.layoutlib.bridge.Bridge.setupTabHost(Bridge.java:880)
at com.android.layoutlib.bridge.Bridge.postInflateProcess(Bridge.java:807)
at com.android.layoutlib.bridge.Bridge.postInflateProcess(Bridge.java:813)
at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:401)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.computeLayout(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(Unknown Source)
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1282)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1267)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1061)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3540)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3161)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
这是与之相关联的
main.xml
文件:<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/basic_tab" />
<include layout="@layout/advanced_tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
这两个include文件单独使用没有问题。但是在标签布局中使用时会出现错误。有解决方法吗?
这是代码:
import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import com.mohit.geo2do.R;
public class TaskEdit extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_task);
Resources res = getResources();
TabHost host = getTabHost();
host.addTab(host.newTabSpec("basic")
.setIndicator("Basic", res.getDrawable(android.R.drawable.ic_menu_edit))
.setContent(R.layout.basic_tab));
host.addTab(host.newTabSpec("advanced")
.setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
.setContent(R.layout.advanced_tab));
host.setCurrentTab(0);
}
}
以下是XML文件,advanced_tab:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/advanced_tab_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Notify me:"
android:textColor="#FFFFFF"
android:textStyle="bold"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/task_due"
android:text="when task is due"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/task_overdue"
android:text="when task is overdue"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/task_datetime"
android:text="at"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Calendar:"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/task_calendar"
android:text="Create a calendar event"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
</LinearLayout>
并且是basic_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/basic_tab_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Title:"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="5dip"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:hint="Name of the task..."
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Importance:"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="10dip"
/>
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:id="@+id/importance_grp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High"
android:paddingRight="25dip"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:checked="true"
android:paddingRight="25dip"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Low"
/>
</RadioGroup>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Due Date:"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="10dip"
/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/due_date"
android:prompt="@string/due_date_prompt"
android:entries="@array/due_date_entries"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Notes:"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:paddingTop="10dip"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/notes"
android:hint="Notes..."
android:minLines="4"
/>
</LinearLayout>
UPDATE:
Apparently:
host.addTab(host.newTabSpec("advanced")
.setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
.setContent(R.layout.advanced_tab));
不需要一个布局XML文件作为它的内容。它应该是:
host.addTab(host.newTabSpec("advanced")
.setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
.setContent(R.id.advanced_tab_layout));
setContent
需要一个id作为参数。尽管在模拟器上仍然会出现错误,但是选项卡实际上已经显示出来了,但是没有内容。