Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:43:21+00:00 2026-05-26T21:43:21+00:00

So I have different layouts for this one Activity. And I have different classes

  • 0

So I have different layouts for this one Activity.

And I have different classes that each open and do their thing with a layout.

I inject these classes in the Activity via @Inject. All this is without problem.

But when I try to use @InjectView on one of the controls that are in the not active layout I get an error.

11-02 19:17:31.086: ERROR/AndroidRuntime(1326): Caused by:
java.lang.NullPointerException: Can’t inject null value into class
be.baes.notes.View.EditNoteImpl.saveButton when field is not @Nullable

This would then be the code.

public class EditNoteImpl implements EditNote {
    @Inject CancelEditNoteClickListener cancelEditNoteClickListener;
    @Inject SaveNoteClickListener saveNoteClickListener;
    @Inject Provider<Activity> activity;
    @InjectView(R.id.saveButton) Button saveButton;

    /* (non-Javadoc)
     * @see be.baes.notes.EditNote#activateEditNote()
     */
    @Override
    public void activateEditNote()
    {
        activity.get().setContentView(R.layout.editnote);

        this.saveButton.setOnClickListener(saveNoteClickListener);
    }
}

I can however do this.

public class EditNoteImpl implements EditNote {
    @Inject CancelEditNoteClickListener cancelEditNoteClickListener;
    @Inject SaveNoteClickListener saveNoteClickListener;
    @Inject Provider<Activity> activity;
    private Button saveButton;

    /* (non-Javadoc)
     * @see be.baes.notes.EditNote#activateEditNote()
     */
    @Override
    public void activateEditNote()
    {
        activity.get().setContentView(R.layout.editnote);
        saveButton = (Button)activity.get().findViewById(R.id.saveButton);

        this.saveButton.setOnClickListener(saveNoteClickListener);
    }
}

Is there a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:43:21+00:00Added an answer on May 26, 2026 at 9:43 pm

    I have just started to use roboguice so someone else with more experience might be able to give a better answer, but this is what I found so far:

    • In roboguice-1.1.2 (the current stable release), @InjectView items are injected only when setContentView() is called the first time in the Activity. Since your code calls setContentView() dynamically from the auxiliary classes, the injected items there won’t be injected correctly.

    • In roboguice-2.0b2 (the current beta), there is support for multiple setContentView() calls and your code should work. However, injected views are still tied to the context Activity (instead of the declaring class) so every @InjectView will potentially also need to be @Nullable across all auxiliary classes sharing the same Activity.

    Since the issue here seems to be rooted in the multiple layout (and hence multiple setContentView() calls) in a single Activity, one alternative way to do what you want is to avoid it as follow:

    • Instead of having several layouts, use a single layout using the <include/> tag to load all the layouts into a parent FrameLayout:
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <include layout="@layout/layout1" />
    
        <include layout="@layout/layout2" />
    
        <!-- other layouts... -->
    
    </FrameLayout>
    
    • then, instead of calling setContentView(), use a method that will toggle the visible layout on the Activity, something like this:
    // instead of: activity.setContentView(R.layout.layout1);
    // use: activity.showLayout(R.id.layoutview1);
    public void showLayout(int layoutViewId) {
        final View view = findViewById(layoutViewId);
        final ViewGroup root = (ViewGroup) view.getParent();
        for (int i = 0; i < root.getChildCount(); i++) {
            final View v = root.getChildAt(i);
            v.setVisibility(v == view ? View.VISIBLE : View.GONE);
        }
    }
    

    The above alternative should work for both the stable and beta roboguice releases. The trade-off here is we are loading several layouts at the same time instead of loading each one at several different times. It seems to work well enough for me (though it might be different for your needs).

    One thing I should note is that on the current “Upgrading from RoboGuice 1.1 to 2.0” page, the following is mentioned:

    The ability to use @InjectView in Views (although you’ll need to call
    RoboGuice.injectMembers() yourself, since there’s no RoboView base
    class and probably never will be).

    It seems that this should allow you to implement your auxiliary classes to be derived from View and make @InjectView in them work better (hopefully no need for them to be @Nullable since they are less tied to the Activity). However, looking at the current code, this feature does not seem to be implemented yet (although I could have been looking at the wrong place).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two layouts for one activity like layout-port and layout-land, with different designs.
I'm completely stumped on this one. I have three different lists that need to
This is different than the simple 2 column layout. I need to have this
I have an Android Activity that needs to catch two different broadcasts. My current
In one of my Activities I do have up to six different AsyncTasks that
So I have one activity with a listview in it that is dynamically updated
I have two different activities. One's view contains RadioButton s that give the user
Suppose I have 3 completely different layouts for one site. The first is shown
I have a program with 4 different tabs. One of these tabs is an
I have a program with 4 different tabs. One of these tabs is an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.