从 XML 膨胀时出现空指针异常(不使用 TabActivity)

发布于 2024-10-08 10:28:48 字数 773 浏览 0 评论 0原文

我目前正在尝试调试我的 Android 应用程序。我已经构建了第一个屏幕,它使用选项卡主机。我对 TabHost 做了一些小的修改,添加了一些额外的方法(完整的源代码)来与我的应用程序的其他部分一起使用。

然而,当我尝试运行以下命令时,我在 Inflate 上遇到了空指针异常,但它并没有真正告诉我任何原因。我下载了 SDK 的完整 Android 源代码,并将其添加到 eclipse 中,以逐步完成通货膨胀过程,并查找是否可以通过这种方式获得有关错误的更多信息。

我确切地找到了导致错误的行(源代码如下),但是当单步执行调试器时,发生了一些奇怪的事情。逻辑是 if/else 情况,其中 if == true 它应该返回一个 New Parser() 对象,我运行了该代码,它成功地单步执行了 Parser 的构造函数,但从该构造函数返回后,下一条指令直接退出语句组到下面的行返回 null。

构造函数似乎已正确运行(如果您想知道它位于 XmlBlock.java 中),但它仍然会进入下一行并返回 null,我不知道这是为什么。我已经寻找解决方案,但到目前为止,使用不适合我的应用程序的 TabActivity 的人遇到了唯一的类似问题。我需要由我在项目中定义的对象(类似于 IOS 风格的视图控制器)生成选项卡及其视图,而不是通过使用意图启动新活动来分离进程。

有人可以看一下我的消息来源并告诉我我在这里做错了什么,因为似乎没有人可以告诉我问题是什么?这是指向所涉及来源的另一个直接链接:http://pastebin.com/9VRE2UGW

I'm currently trying to debug my Android App. I've built out the first screen and it's using a Tab Host. I had made a few minor modifications to TabHost to add a few extra methods (full source to follow) to work with some other parts of my App.

However, when I try to run the following I get a null pointer exception on Inflate and it doesn't really tell me anything about why. I downloaded the full Android source for my SDK and added it in eclipse to step through the inflation process and find if I could get any more info about the error that way.

I found exactly what line was causing the error (source included below) but when stepping through the debugger something strange happened. The logic is an if/else case where if == true it should return a New Parser() object, I ran that code and it successfully stepped through the constructor of Parser, yet after returning from that, the next instruction stepped right out of the statement group to the line below returning null.

The constructor appears to have run properly (it's located in XmlBlock.java in case you're wondering) yet it still steps to the next line and returns null and I have no idea why this is. I've searched for solutions but so far the only similar problem was encountered by someone using a TabActivity which is not suitable for my Application. I need Tabs and their views to be generated by objects I've defined within my project (similar to IOS style View Controllers) and not separate processes via using Intents to launch new Activities.

Can someone please have a look at my source and tell me what I'm doing wrong here because no one else can seem to tell me what the problem is? Here's another direct link to the source involved: http://pastebin.com/9VRE2UGW

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-10-15 10:28:48

好吧,看来我真正的问题是我没有在 Android DDMS 视角中查找调试信息。我最初认为 Java 调试视角会给我所有必要的信息。这个异常的真正原因是在程序的其他地方。现在我正在阅读 logcat,我应该能够自己解决剩下的问题,因为这是我真正的问题(信息不足)。

Okay, well it looks like my REAL problem here was that I wasn't looking in the Android DDMS perspective for my Debug info. I'd originally thought the Java Debug perspective would give me all the necessary info. The real cause of this exception was elsewhere in the program. Now that I'm reading the logcat I should be able to solve the rest of this on my own since that was my real problem (inadequate information).

放低过去 2024-10-15 10:28:48

我怀疑这一行是错误的:

TabBarController UITabBarController = 
    (TabBarController)interfaceBuilder.inflate(R.layout.tab_bar , null);

因为您的 R.layout.tab_bar 代表 TabHost

<?xml version="1.0" encoding="utf-8"?>
<TabHost
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@android:id/tabhost">
        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                <TabWidget
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@android:id/tabs"
                android:layout_alignParentBottom="true" />

                <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@android:id/tabcontent">
                </FrameLayout>
        </RelativeLayout>
</TabHost>

I suspect this line is wrong:

TabBarController UITabBarController = 
    (TabBarController)interfaceBuilder.inflate(R.layout.tab_bar , null);

Because your R.layout.tab_bar represents a TabHost:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@android:id/tabhost">
        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                <TabWidget
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@android:id/tabs"
                android:layout_alignParentBottom="true" />

                <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@android:id/tabcontent">
                </FrameLayout>
        </RelativeLayout>
</TabHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文