Null Pointer Exception when inflating from XML (Not using TabActivity) - android

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

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

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>

Related

Inflating all layouts from res-folder

There are 100+ layouts in a resource folder on android, I can go through them one by one and look at the preview in android studio. They all utilise code like:
tools:text="text for preview"
tools:visibility="visible"
Is there a way to inflate all those one after another in a list/linear layout? And upgrade the tools:xyz attribute to be shown as the real android:xyz attribute in this view
Context&Purpose: To show them in the app under a debug menu dedicated specifically to show available layouts. This is to communicate with both other developers and people without android studio.
I want to avoid doing it manually as it needs to be maintained and that can end very badly for debug functionality such as this. (And that it is a lot of layouts does not help either of course)
After writing it down it feels unlikely, but maybe someone out there have a nice angle on this?
Depending on the complexity of each layout, inflating a hundred of them at once could be quite taxing. You may run out of memory, or get a fair bit of jank. If you are OK with this, then I would say try using the include tag in your debug screen layout
A simple/naive implementation would be something like
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/A"/>
<include layout="#layout/B"/>
<include layout="#layout/C"/>
<include layout="#layout/D"/>
...
</LinearLayout>
I probably wouldn't recommend doing this, but it might get the job done.
As far as the second part of your question, using tools values as actual values, I don't think there is a way to accomplish this. In fact the tools values are not even available during run time as they are stripped out during the build process.

Android Layout Editor Freaks Out on Question Mark

This is strange, yet I see it all the time as I have lots of reasons to display just a simple question mark in this app.
When editing with the Eclipse graphical layout editor, everything is fine...until I tell a TextEdit or a Button to display just a question mark. Here's my code:
<?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"
>
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question_mark" />
</LinearLayout>
And the string is defined in res/values/strings.xml. The relevant line is:
<string name="question_mark">\u003f</string>
The error message I get is:
Missing styles. Is the correct theme chosen for this layout? Use the
Theme combo box above the layout to choose a different layout, or fix
the theme style references.
Couldn't find theme resource for the current theme
Change the text, and the error message goes away.
You can see that in my struggles, I'm even trying to use the unicode version of a question mark. And yes, \? doesn't work either.
Note that this only happens when the graphical layout editor is set to API 7 or greater.
Now the graphical layout editor displays the question mark properly, and the emulator and my phone display the question mark without any problems. I'm just annoyed with the error message taking up 1/4 of my screen for all my layouts (and obscuring other error messages that may crop up).
Any suggestions?
First, test this again on the ADT 21 release that shipped today.
If the problem continues, create a sample project that demonstrates the issue, and post it along with step-by-step instructions on the Android issue tracker.
Well, this is a really crappy hack, but it kind of works--if you stand on your head!
You can use the unicode \u00bf as in
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u00bf" />
It's not a normal question mark, but it's close enough for a hack and gets that annoying error message out of your hair. And it might even increase the humor quotient of your app!
But PLEASE, if anyone out there has a real fix, post it!
Wow, I entered a bug report...[time marches on]...finally, I get a few emails as the Google team starts to look at it.
Today, I received some good news. It looks like the bug has been fixed (and they found a few related bugs, which have been fixed as well). The fix will be in the next release, Version 21.1 Preview 2. You can read the official details here.
Looking forward to it!

ActionBarSherlock + Google Maps API, Failed to find style 'mapViewStyle' in current theme

First of all, I know there are thousens of post asking the same, but after few days looking for a valid answer and try everything I'm still having the same problem...
So let's expose my situation:
I'm using ABS + google maps, so my map view xml is the following:
<?xml version="1.0" encoding="utf-8"?>
<com.actionbarsherlock.internal.view.menu.ActionMenuItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0PGmnrCR70lothfjc_sFGTdFtESQPOksYKQWYpw"
android:clickable="true" />
</com.actionbarsherlock.internal.view.menu.ActionMenuItemView>
Few days ago everything was good, I was able to do my stuff in the app and everything. But suddenly I just made a little change, insert a framelayout to display more options in the screen. I tried to go back but was impossible...
I'm really deseparated, what can I do? maybe I can create a new project and paste all my code, but not really sure if it's going to work...
Any suggestions?
BTW my error code is this one
Missing styles. Is the correct theme chosen for this layout?
Use the Theme combo box above the layout to choose a different layout, or fix the theme style references.
Failed to find style 'mapViewStyle' in current theme
java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log
Thank you for your time!
I finally solve the problem, the error in the screen is still there, but just like a warining and when I execute the app same to work fine.
The only think I made, is change the specific layout of ABS and put a frame layout.

Non-urgent Android layout id attribute quirk

I'm working on a couple of apps at the moment while I try to learn my way around the Android SDK. I had a bit of trouble recently with my layouts where I was defining, for example, an EditText element as such...
<?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">
<EditText
android:id="#+id/price_per_pack"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/price_per_pack"
android:layout_alignParentLeft="true"
android:textSize="12pt"
android:text="Price Per Pack"/>
</LinearLayout>
The application was compiling correctly, however when I attempted to start the activity which utilizes this layout nothing would happen. Through a process of elimination I identified the id attribute as the troublesome one and while playing about I discovered that changing
android:id="#+id/price_per_pack"
to
android:id="#+android:id/price_per_pack"
solved my problem and the application behaved as expected. My initial attempt at declaring the id attribute was based upon examples in the SDK documentation so I'm wondering if somebody could explain to me why I needed to make the above change to get it working?
I'm sure that it won't make any difference but I'm developing using the android-mode.el emacs plugin and have a completely up-to-date copy of the SDK.
Thanks in advance.
From Android Documentation:
With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.
It looks like you might have some syntax errors in your code posted.

My Android "Hello World" app doesn't say "Hello"

I created a Hello World application, and the system generated most of the Android language below. When running the app without the System.out statement, there is no "Hello" displayed in the emulator. Then, using the Eclipse tutorial, I read that I can add the System.out.println statement to main. Again the app runs, but there is no output.
What am I not understanding here?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
System.out.println =" Hello world!"
/>
</LinearLayout>
I had this same problem, turns out all I had to do was be more patient.
The initial load time of the AVD seems like FOREVER... and in Eclipse before it actually looks like the device is booting up it just says "ANDROID_" against a fully black screen for quite a while. I thought it wasn't loading my text "Hello, Android" correctly, but it was actually just a load screen and I didn't wait long enough. (Yes, I now feel like a moron and a noob.)
If you're following along with the Android Developers Hello World Tutorial, and it doesn't seem like it's working, if you've never loaded the AVD before... maybe just wait longer?
Beginners' questions are fine, but boy, this really is a beginner question :) As for your problem, it can be a few things. What's probably happened is that you've adapted the basic "Hello, Android" tutorial which defines the TextViews in code, to make it display using an XML file. However, when you did that, you didn't change the code to use that XML file, and instead it's trying to display your old TextView. Also, "System.out.println="hello world!" won't do anything when in your XML file - you need to put statements like that in the code itself. In fact, offhand I can't remember if System.out.... even does anything in Android - debugging lines should be issued using Log.d("some title", "your message"), as that outputs to the Android specific logging device.
Anyway, it'd be easier to help solve your problem if you showed a bit more of your code. Try to make sure it's formatted properly, e.g. indenting code lines by four spaces. You can preview your post before you submit your edited version in the lower window to make sure it looks right.
Try this.
Hardcoded in [your_layout].xml
...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Hello world" />
or this
Use reference in [your_layout].xml
...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#strings/hello" />
In res/values/strings.xml
<resources>
<string name="hello">Hello world</string>
</resources>
That should help you get started a bit. A few thing to remember, System.out.println doesn't do a thing in Android, especially in XML file.
When addressing text with "#string/hello", it means that the application will look for string name "hello" inside strings.xml.
Anyway, you should try Android tutorial to get start.
One thing is for sure, putting println in your layout resource file won't do anything. This code(if it may be called so) is NOT executed. This is just definition of view. If at all, this statement should be called from your Activity class implementation(the java file) to be executed.

Categories

Resources