尝试打开 Android XML 视图时 Eclipse 中抛出 NullPointerException?

2024-06-26

这是例外情况:

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));

Where setContent需要一个 id。我仍然收到该错误,但在模拟器上,选项卡实际上显示,但没有内容!


您列出的例外是布局编辑器本身的错误。运行 Android 布局代码时,我们没有正确初始化 SDK 版本,这意味着 TabWidget 中的一些版本条件代码将运行(这会失败,因为它实际上并未在它认为的版本中运行)。我们正在 ADT 10 中修复此问题(以及选项卡的各种其他渲染/预览问题。)

(This https://review.source.android.com/#change,20562是查找正确版本以传递给渲染器的变更集)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试打开 Android XML 视图时 Eclipse 中抛出 NullPointerException? 的相关文章

  • android:onClick 在 xml 中的 menuitem 上设置

    亲爱的 我们可以在 xml 中将 onclick 与菜单项一起使用吗 就像下面一样 我尝试过但它不起作用 menu menu
  • Android - 缩放和压缩位图

    我正在开发一个 Android 应用程序 它具有相机捕捉和照片上传功能 如果设备具有高分辨率相机 则捕获的图像尺寸将非常大 1 3MB或更大 由于应用程序需要将此图像上传到服务器 因此我需要在上传之前压缩图像 例如 如果相机拍摄了 1920
  • 域不包含在应用程序域中

    我几天来就遇到了这个问题 以前它运行得很好 但现在显示了上述错误 我无法找出我缺少的内容 如何解决这个问题 image setting image 的一部分费率审查 public static String BASE DOMAIN http
  • 透明模糊视图模糊了下面的布局

    我有一个已设为透明的 Linearlayout 现在我正在寻找一种方法来赋予它模糊效果 因此它下面的内容会变得模糊 就像 Windows 7 Aero 外观一样 参见屏幕截图 我知道你可以做这样的模糊效果 getWindow addFlag
  • eclipse mylyn 与 redmine

    是否可以将mylyn连接到redmine而不需要redmine中的rest支持 有一个链接http download eclipse org mylyn incubator 3 8 http download eclipse org myl
  • 如何在线性布局上制作波纹效果,而不覆盖其子级的背景颜色?

    I have a LinearLayout that looks like this 我希望每一行都是可点击的 这LinearLayout一行的代码如下所示
  • 即使在 Scaffold 中定义了 BottomModalSheet 小部件,Flutter 中也未找到 Scaffold 错误

    我试图为测试应用程序实现 BottomModalSheet 但每次都会弹出同样的错误 说找不到脚手架 该应用程序的代码如下 该错误表明 Scaffold 是由 MaterialApp Widget 实现的 因此我删除了 MaterialAp
  • 如何在改造中在主体内传递 JSON 数组

    intent sale redirect urls return url http example com your redirect url html cancel url http example com your cancel url
  • 画廊新媒体如何播放?

    我试图收到有关添加到手机图库的新图片或视频的通知 我需要获取新媒体的 URI 目的是让我可以自动备份它 因此 我需要一个在后台设置的寄存器来连续侦听或检查添加到图库的新媒体 并捕获 Uri 这过去是通过广播接收器完成的 例如
  • 面临 process.start(); 的问题在 Android 棒棒糖中

    面临一个问题process start 在 Android 棒棒糖中 我在服务中遇到了 android lollipop 后台进程的问题 我的代码在 KitKat 之前工作正常 我有一个ProcessBuilder pBuilder并向其中
  • RecyclerView:调整项目大小的动画

    我有一个RecyclerView 回收者视图的每个项目都可以展开或不展开 同一时间只能展开一项 本质上 我试图在棒棒糖拨号器中重新创建历史列表 我发现在 RecyclerView 上使用 LayoutTransition https sta
  • 让 DrawerLayout 在 ActionBar 上滑动

    我在活动中有一个滑动抽屉菜单 其中有一个带有一些选项卡的操作栏 我想让滑动抽屉滑过标签 而不是滑过标签下方 这就是现在的样子 关于如何做到这一点有什么想法吗 注意 我知道我可能会在这里打破一些约定和 UI 模式 如果它根本不起作用 我会考虑
  • Android Google plus 注销按钮看起来与 Google 教程文档中的登录按钮不同

    我正在按照 Google 开发人员网站中提到的步骤在我的应用程序中实现登录和注销 如前所述 添加登录和退出按钮的代码是
  • 如何在 Eclipse 构建期间忽略/阻止验证 javadoc 文件夹?

    在我的战争中有一个巨大的javadoc文件夹 验证它是没有意义的 因为 javadoc 是由 Sun Oracle javadoc 实用程序生成的 我已经忘记上次是怎么做的了 我需要告诉 Eclipse 构建不要验证该特定文件夹 我需要它的
  • 是否可以通过 MediaRecorder 或其他类获取当前样本幅度

    我有一个媒体录音机 想要从麦克风录制媒体并获取 它是幅度样本 我想尝试立即获得正确的当前幅度 当调用一些API时 但 MediaRecorde 中只有一个 API 用于获取幅度 getMaxAmplitude 用于获取最大绝对幅度 自上次调
  • 如何在 Android 键盘中包含建议

    我正在开发 Android 软键盘 我已经创建了键盘布局 但不知道如何包含在 EditText 中键入一些单词时出现的建议 例如 如果我写 在 中 已知 和 已知 将显示在建议中 所以我的问题是 1 如何在 Android 软键盘中包含建议
  • 将文本文件写入 SD 卡失败

    我遇到了一个奇怪的问题 我的应用程序可以将一个简单的文本文件写入 SD 卡 有时它对某些人有效 但对其他人无效 我不知道为什么 对于某些人来说 如果他们输入一些字符 例如 在文件等中 我似乎无法重现它 因为我没有遇到任何麻烦 但这是处理文件
  • Android:Html 锚链接仅在 Web 视图中有效一次

    在使用锚链接加载 html 内容时 我在 webview 中遇到一些奇怪的问题 以下代码非常适合锚标记 但是只有一次 第二次当我按下锚标签时不工作 protected void onCreate Bundle savedInstanceSt
  • 将我的应用程序添加到“添加快捷方式”列表,以便在主屏幕上有快捷方式

    如您所知 当您长按主屏幕时 手机会显示列表菜单 您可以添加快捷方式 小部件 文件夹等 我希望我的应用程序位于快捷方式列表中 我怎样才能做到这一点 快捷方式自 API 级别 1 起就已存在 并且也可由 3rd 方应用程序使用 要将活动添加到快
  • 如何在流体宽度表中使用省略号而不使每列大小相同?

    假设我的表中的列是id name description and phone The description列的长度为 1 255 个字符 但 id 最多只有 3 个字符 我希望列的大小适当 而不是每列的大小相同 我想要descriptio

随机推荐