Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Android Self-Destructing App question

The action bar is missing in my add/delete friends display screen. My EditFriendsActivity is extending ListActivity, and when I attempt to utilize the setupActionBar function as defined by:

private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

   }

I get lots of errors in logcat when I navigate to that page. I have cut and pasted the code from the project files as well as built them via the course and am at a loss as to where to look. The app runs fine in all other aspects, I'm just missing the action bar on that one screen.

Building the project in Android Studio 1.0.2.

Any suggestions would be much appreciated.

Thanks,

Ken

Ken Alger
Ken Alger
Treehouse Teacher

Code is on GitHub here.

Further information:

Target device: Android442_API_19 [emulator-5554]

You will notice in the code that EditFriendsActivity is extending ListActivity. The rest of the project is extending ActionBarActivity. When I change that in EditFriendsActivity all of the calls for List View related items become errors.

In EditFriendsActivity I have the setupActionBar call as well as the function definition commented out so that I can add/delete friends. With those lines of code commented out, EditFriendsActivity runs fine, just no action bar/menu.

When the code is uncommented, the project compiles fine, all other aspects work until I navigate to Add Friends in the menu and then the app stops and in LogCat I get the following error:

Logcat.Error
12-29 20:05:02.269    1191-1191/com.zyzzyxtech.ribbit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.zyzzyxtech.ribbit, PID: 1191
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zyzzyxtech.ribbit/com.zyzzyxtech.ribbit.EditFriendsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.zyzzyxtech.ribbit.EditFriendsActivity.setupActionBar(EditFriendsActivity.java:100)
            at com.zyzzyxtech.ribbit.EditFriendsActivity.onCreate(EditFriendsActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Ben Jakuben took a look at the code earlier and suggested uncommenting thesetupActionBar(), but like I said, it results in the above error.

I'm a bit stuck and would love some assistance. Anyone else ever get code blurry after staring at a project problem too long? I'm sure someone will look at this and say, "Hey, you are missing a FooBar in your BazQux class and that is the cause of all of the problems!" At which point I will slither away feeling foolish, but more knowledgeable.

Thanks for any assistance.

Ken

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I cloned your project from GitHub and ran into an issue where the layout file activity_edit_friends.xml was missing. Can you add that to your repo? It may be different than mine. I added my own layout file from the course project files and got a different error about setDisplayHomeAsUpEnabled() being called on a null reference.

This new error is happening because of some trouble in your project caused by inclusion of the support library. I'll need to take a further look and see the best way to iron out this issue, but we'll get there!

Ken Alger
Ken Alger
Treehouse Teacher

GitHub has been updated, sorry about that, don't know why that file was missing. Must be something in the water here in Oregon. I have been working my way through the project this evening, and everything was progressing along as per the video until I got to the RecipientsActivity section and wouldn't you know it but that Action Bar function caused another NullPointerException.

In the SDK in the

private void setupActionBar() {
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

the code is "highlighted" and on hover I am warned that Method invocation of getActionBar().setDisplayHomeAsUpEnabled(true); may produce a java.lang.NullPointerException. Not sure if you are getting the same error(s) as I have been getting, but somewhere my system doesn't like it.

Out of curiosity, I have noticed that in just about every Java class file I create I wind up with multiple unused imports. Can that be attributed to the way Android Studio 1.0.2 creates classes or am I doing something odd?

Thanks for all you do.

Ken

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hey Ken! I had a chance to revisit this today and just submitted a pull request to your repo. Basically you ended up in a weird spot where using stuff from one of the support libraries causes issues with ListFragments. The support libraries can be a real pain sometimes. I don't even know how to explain it except to say that you need to make certain changes based on which version of a support library you are using. I updated your project to use the v13 support library for the FragmentPagerAdapter which allows you to use regular Activities (not ActionBarActivities) and also standard Fragments (not the support versions).

Check the code and let me know if it works or if you have any follow-up questions about it. Sorry for the trouble!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hey Ken! I had a chance to revisit this today and just submitted a pull request to your repo. Basically you ended up in a weird spot where using stuff from one of the support libraries causes issues with ListFragments. The support libraries can be a real pain sometimes. I don't even know how to explain it except to say that you need to make certain changes based on which version of a support library you are using. I updated your project to use the v13 support library for the FragmentPagerAdapter which allows you to use regular Activities (not ActionBarActivities) and also standard Fragments (not the support versions).

Check the code and let me know if it works or if you have any follow-up questions about it. Sorry for the trouble!

Ken Alger
Ken Alger
Treehouse Teacher

Ben Jakuben;

No trouble on my end at all, thanks for your help! The corrected and updated files work great. I'll add this to the learning process of Android development.

Questions about this issue:

  1. Was this caused by something specific or is this more of an undocumented feature of Android Studio vs. Eclipse?
  2. Any good resources for learning about these issues?
  3. Now that I am back on track, what do I need to look for as I proceed to insure that I don't get buggered up again?

Thanks again and Happy New Year.

Ken

Ken Alger
Ken Alger
Treehouse Teacher

Mr. Jakuben;

In working through the rest of this project I am continuing to have issues, mostly import related. Should I now be using the v13 support library for things such as the SwipeRefresh and GridView? I cannot get either of them to work straight from the video, if at all.

Thanks,

Ken

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi again Ken,

Sorry to have dropped this thread here! I must have missed it with time off for the holidays. Are you still having trouble with this project?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi again Ken,

Sorry to have dropped this thread here! I must have missed it with time off for the holidays. Are you still having trouble with this project?

Ken Alger
Ken Alger
Treehouse Teacher

Mr. Jakuben;

Unfortunately, yes. I made it through the Ribbit course and everything wound up working fine. Got into the design portion of it and import errors seem to be creeping back into things. Perhaps I am just too punchy when I organize my imports, but it seems like I have imported different support libraries that conflict with one another or just the wrong ones.

I will be getting back into the UI course later this week and will probably download the course files and then spend time comparing the two. When I did that last time it seemed like every correction I made in my code to match the course code resulted in multiple new errors for me.

Lots of questions about this project have come up for me and I'm not sure if they are due to something I have done incorrectly, a difference in dev environment, or it has been some evil electrons messing with me. Everything from a difference in what MainActivity is extending, to those lovely support libraries, to fragment managers. I think part of my frustration is understanding the concepts and knowing in general what needs to be done, but not knowing the how to get it to do it or correct the error(s). Probably the only one in development history that has ever had that experience, right? :smile:

Thanks for posting back and your willingness to assist.

Ken

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ben Jakuben

I am still having issues with this app and the import libraries. Would you mind sharing the technique you used to resolve the issue with my project the first time? I'd like to try debugging everything but am a bit lost on what libraries should be used and which ones should go away.

Thanks, Ken

Ken Alger
Ken Alger
Treehouse Teacher

Ben Jakuben

Spent some time digging into the import issues this evening and between the helpful clues Android Studio provides, Google, Stack Overflow, and trial and error I was able to fix everything and it runs just dandy now.

Ken

Ian Z
Ian Z
14,584 Points

im super stuck on this too, but i cant make sense of what you guys are saying, i tried extending ActionbarActivity and then doing: protected ListView mListview = (ListView) findViewById(R.id.receipientsListView);

but then i cant do onListItemClick method or mListview.onListItemClick

so i went back to doing extend ListActivity but now setupActionBar(); gives cannot resovle method

what do i do?

Sam Coles
PLUS
Sam Coles
Courses Plus Student 13,887 Points

Ian Z - did you manage to fix this issue? Any advice would be appreciated

Ken Alger
Ken Alger
Treehouse Teacher

Sam;

How can I help?

Ken

Sam Coles
PLUS
Sam Coles
Courses Plus Student 13,887 Points

Hi Ken,

I too am having great difficulty adding the toolbar to my add/delete friends display screen.

My EditFriendsActivity is extending ListActivity.

I'm using Android Studio 1.1.0 and my SDK is v22

I have tried the solutions that Ben and others have suggested on the forum but I cannot get it to work.

I have uploaded my project to Github. Perhaps you could take a look and let me know what I need to add/change in my files to get the toolbar working? Github - https://github.com/scoles2428/PictureThis

Thanks Sam