這裏是例外:嘗試打開Android XML視圖時在Eclipse中引發NullPointerException?
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>
兩個包含文件單獨工作就好了。但是當在標籤佈局中時,會發生此錯誤。任何修復?
下面是代碼:
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:
顯然:
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。我仍然得到這個錯誤,但在模擬器上,這些選項卡實際上顯示,但沒有內容!
請上傳驗證碼。我沒有在您的XML中看到任何TextView。也許你已經把它們放在'basic_tab'或'advanced_tab'文件中。發佈完整的東西的人! – 2011-01-14 07:33:25