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 Build a Self-Destructing Message Android App Starting the App Adding the Login Screen

ActionBar deprecated - NullPointerException - how do I work with AppCompat v7?

Will my code still work properly?

public class MainActivity extends AppCompatActivity {

and when I change it to what Ben has it at, I get an error

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

13 Answers

Steve I fixed the project!! all I had to do was when creating the new activity choose empty activity instead of blank activity, because when I choose blank activity it created that random content_login.xml file that was messing everything up and causing my app to crash when I run the LoginActivity.class off of the intent.

all the tabs are working correctly as well.

Good work - well done! That was the first change I made to the code - I amended ActionBarActivity or AppCompatActivity to just a simple Activity but, as you say, the clutter caused by using the generated code gets in the way. Starting that from scratch would avoid that issue, though.

The only thing is that when I press the back button from the login screen it takes me to the tabs and i have that little mail icon on the bottom of the screen, do i want that there? Or how do i remove it?

Isn't there some FLAGS property to prevent the back button doing that? I'm not near the Mac at the moment, so can't check for the line that does that.

It is something like Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TASK.

That will get rid of that little mail icon on the bottom right of my screen when I go to the tabs page by pressing the back button? I can't find the mail icon in any of the xml files either

I doubt it - that'll just disable the navigation ability to go 'back' to a screen. So, if you've logged out, that takes you to the login screen. The use of those FLAG properties prevents hitting 'back' to revert to a logged in state,

I've no idea where your icon is coming from. What files do you have in your resources? One of those must be a mail icon and something must be deploying it into your activity.

Oh haha I just found it, it's in my activity_main.xml file.

I just took a look and here's what I have in activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

In the downloadable project zip files it only has:

<?xml version="1.0"?>
<android.support.v4.view.ViewPager
    tools:context=".MainActivity"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/pager"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

Should I keep all of the previous code that was already generated for me, or will I be ok with just this?

Never mind I have to keep the previous code otherwise I get an error when running, but will I still be ok with all of that code inside activity_main.xml?

I think it's trying to tell me I have a nullpointerexception, but I don't know where. Let me know which files you need to see if you're able to help me out.

The error messages are pretty opaque! The beginning tells you what the chief gripe is, as you say it's a NullPointerException, then much further down you can find an error that relates to your own code - com.teamtreehouse.ribbit.LoginActivity.onCreate(LoginActivity.java:17) - as in my answer below, I'm guessing that line is about the action bar.

Hi Derek,

The ActionBar was deprecated a couple of versions of Android ago. So now the application compatibility activity mimics the deprecated behaviour.

I think you should be OK to just carry on and ignore that - the new activity should work the same way. But in case you do run into odd issues, just ask on the Community pages and you'll get the help you need.

Steve.

OK thanks, but right now I am stuck because my app keeps crashing every time i try to run it, here are the errors I'm recieving.

09-21 04:55:05.736 5688-5688/com.teamtreehouse.ribbit E/AndroidRuntime: FATAL EXCEPTION: main 09-21 04:55:05.736 5688-5688/com.teamtreehouse.ribbit E/AndroidRuntime: Process: com.teamtreehouse.ribbit, PID: 5688 09-21 04:55:05.736 5688-5688/com.teamtreehouse.ribbit E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamtreehouse.ribbit/com.teamtreehouse.ribbit.LoginActivity}: java.lang.NullPointerException

  • snip * 09-21 04:55:05.736 5688-5688/com.teamtreehouse.ribbit E/AndroidRuntime: at com.teamtreehouse.ribbit.LoginActivity.onCreate(LoginActivity.java:17)
  • snip *

Yes, I remember this issue before ...It think this is due to your ActionBar.

There's a NullPointerException - and it is probably at line 17 of LoginActivity - is that line getActionBar(), perhaps?

Let me do some digging - someone cleverer than me (a pretty low bar!) figured out a solution to this a while back.

Steve.

yup you're correct sir, on line 17 of LoginActivity.java

setSupportActionBar(toolbar);

I can't find the thread that had to solution in it. There are many others circling the issue:

https://teamtreehouse.com/community/not-able-to-get-the-share-action-icon-in-the-action-bar

https://teamtreehouse.com/community/android-menu-action-bar

But there was one definitive one that I'm not able to track down. From memory, the clever guy suggested using extends Activity rather than the AppCompat one. I'll keep looking ...

Let's call for help - Ken Alger Ben Jakuben Maciej Czuchnowski Craig Dennis

There's a theme issue here too ... can you change your theme to one of the holo ones? (This is coming back to me very slowly, I apologise!!). The deprecated action bar can be worked around by changing the theme to Holo.Light.NoActionBar? I'll leave it to the experts as I've not been in and around this issue for a long time and am not recalling the solution well at all.

When I go into my manifest and add

android:theme="@style/Holo.Light.NoActionBar" >

it throws an error

And when I try to extend Activity inside of MainActivity.java I get 2 other errors:

public class MainActivity extends Activity {

Here are my 2 errors:

setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

Here's the full block of code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

I'd change that back in the Manifest and change the theme by going to the graphical editor of MainActivity.xml.

Double click the res | layout | activity_main.xml. Selected the Design tab at the bottom of the graphical window. Up at the top there's a circle that half filled-in. That the theme selector. Amend it in there.

Steve.

The best way of doing this would be to upload your project into Github or such - then I can replicate the issue at this end and see if I can figure out a solution rather than wildly guiding you all over the place!!

Alternatively, you could email your project over, but that can bring its own issues. Sharing over Github or BitBucket is much cleaner.

I currently don't have a github account I will make one tomorrow and try to upload my code for you then.

Perfect - I'll do what I can to solve it. I'm away on business from tomorrow afternoon to Thursday morning but may be able to spend some time on this, depending on the hotel wifi.

On an off note I had forgotten where to find that theme selector menu so thank you for that! But when I change it to holo.light.noactionbar I get a rendering problem.

Also my activity_main.xml looks a lot different from the project files I downloaded off of the lesson and I'm not sure why it added all of this extra stuff.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

That's adding a load of stuff to mimic the action bar! How ironic.

What's the rendering problem show up as?

First I get these exception details, and I'm very sorry but I don't have a github account yet I will make one tomorrow so I can make this a lot easier for those helping me out.

java.lang.NullPointerException at android.support.design.widget.FloatingActionButtonImpl.createBorderDrawable(FloatingActionButtonImpl.java:74) at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:65) at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:135) at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:79) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:458) at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:170) at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:103) at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:190) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:218) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:145) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) at android.view.LayoutInflater.rInflate(LayoutInflater.java:811) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:394) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:223) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426) at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498) at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498) at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72) at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610) at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607) at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:366) at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607) at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629) at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310) at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227) at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217) at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238) at com.intellij.util.Alarm$Request$1.run(Alarm.java:351) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Above the exception details it says:

Rendering Problems 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.

NOTE: One or more layouts are missing the layout_width or layout_height attributes. These are required in most layouts.

Or: Automatically add all missing attributes (which didn't do anything)

The following classes could not be instantiated:

android.support.design.FloatingActionButton

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE.

That little lot, I am afraid to say, means very little to me at all! It would seem that the solution to the underlying problem may lie elsewhere!

Honestly I just wish Ben would update these videos to android studio like the other apps we've made. Thank you for your help so far I really appreciate it.

I'm sure it is in the pipeline - it must be a nightmare trying to keep on top of all the updates. But, yes, this course does suffer more than others with the progression of the Android SDK. An update would be great!

Should I just wait until one of the others that we called to help reply to this thread? Because I'm not sure how to continue these lessons.

I've amended the question title to hopefully bring in some solutions. If you work towards getting Github set up, that'll help for all projects. I'll continue digging for answers at this end. Hopefully, the experts will reply soon enough.

Ok I will get that set up (:

juliendebray
juliendebray
1,281 Points

Hi, any update on this issue? The getActionBar() return null in my MainActivity.java? Did you find an answer ?

Wait are you also getting the nullPointerException?

Spent a few hours on this - made some progress then broke all that. I did have your tabs working, though!

There must be a fix for this ridiculous problem so I will keep trying. This may involve me going through the course again; we shall see.

Steve.

Hey can you explain to me why it created 2 xml files for me when I made the LoginActivity class. It made the content_login.xml and activity_login.xml. And why can I actually see the layout when I put the login layout inside of content_layout.xml instead of activity_login.xml. When I put the login layout inside of the activity_login.xml and then I call the intent off of LoginActivity.class the app crashes, but not when the layout is in the content_layout.xml file.

Can you explain why these things happen.

My tabs are working but when I run the app the login screen is pushed up to the very top of the screen, so much that the username text field is pushed into the action bar. I ran my app on a nexus 4 api 19 4.4.4

I didn't see this before but now I understand why I can see my login screen when I place the login screen layout inside of content_layout.xml

This is activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.teamtreehouse.ribbit.LoginActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_login"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

It's that include layout property, that allows the content_layout.xml file to show when I call the intent off of LoginActivity.class

<include layout="@layout/content_login"/>

should there be a way for me to place the login screen layout inside of activity_login.xml without my app crashing when I call the intent off of LoginActivity.class or is there a way for me to edit these properties to make my content_login.xml format correctly when I run it on the emulator?

Ok just let me know if you need me to do anything thanks Steve

A thing to help folks get started since Android studio 1.4 came out and they changed the New Project UI again and we have no NAV type option now is to provide a zipped package of files for overriding the starting activity (i.e. activity_main.xml, MainActivity.java)