尝试打开 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>
更新:
显然:
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。我仍然收到该错误,但在模拟器上,选项卡实际上显示,但没有内容!
Here is the exception:
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)
Here is the main.xml
file it's associated with:
<?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>
Both the include files work individually just fine. But when in a tab layout, this error occurs. Any fixes?
Here is the code:
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);
}
}
And here are the xml files, 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>
And 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));
Doesn't take a layout xml file to be it's content. It should be:
host.addTab(host.newTabSpec("advanced")
.setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
.setContent(R.id.advanced_tab_layout));
Where setContent
takes an id. I still get that error, but on the emulator, the tabs actually display, BUT WITH NO CONTENT!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您列出的例外是布局编辑器本身的错误。运行 Android 布局代码时,我们没有正确初始化 SDK 版本,这意味着 TabWidget 中的一些版本条件代码将会运行(这会失败,因为它实际上并未在它认为的版本中运行)。我们正在 ADT 10 中修复此问题(以及选项卡的各种其他渲染/预览问题。)
(这是查找正确版本以传递给渲染器的变更集)
The exception you are listing is a bug in the layout editor itself. When running the Android layout code, we were not properly initializing the SDK version, which meant some version-conditional code in the TabWidget would get run (which fails because it's not actually running in the version it thinks). We're fixing this in ADT 10 (along with various other rendering/previewing issues for tabs.)
(This is the changeset which looks up the right version to pass to the renderer)
好吧,我刚刚清理了项目并删除了
edit_task.xml
文件并重新创建了它。现在效果很好。Well, I just cleaned the project and deleted the
edit_task.xml
file and recreated it. It works fine now.最后有一个解决方法,安装Android 3.0 Preview SDK,可以将布局编辑器中的Render Target设置为3.0。
更多信息请点击这里-> http://code.google.com/p/android/issues/detail ?id=13092
finally a workaround exists, install the Android 3.0 Preview SDK, you can set the Render Target in the layout editor to 3.0.
More info here-> http://code.google.com/p/android/issues/detail?id=13092