1

I am building an Android app using a sliding menu like Facebook but the ViewGroup doesn't work properly. It works fine on my phone running 4.1 but when I tried to run my code on emulator running 2.3, mContent = (FrameLayout) findViewById(R.id.animation_layout_content); this line always returns null; however, mSidebar returns the view correctly. I would really appreciate it if someone help with this problem. Thank you!

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_singlepane_empty);

    if (getIntent().hasExtra(Intent.EXTRA_TITLE)) {
        setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));
    }

    final String customTitle = getIntent().getStringExtra(Intent.EXTRA_TITLE);
    setTitle(customTitle != null ? customTitle : getTitle());

    if (savedInstanceState == null) {
        mFragment = onCreatePane();
        mFragment.setArguments(intentToFragmentArguments(getIntent()));
        getSupportFragmentManager().beginTransaction()
                .add(R.id.animation_layout_content, mFragment, "single_pane")
                .commit();
    } else {
        mFragment = getSupportFragmentManager().findFragmentByTag("single_pane");
    }

    mLayout = (AnimationLayout) findViewById(R.id.animation_layout);
    mLayout.setListener(this);
}


layout.activity_singlepane_empty
<com.oldroid.apps.planit.util.AnimationLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<FrameLayout
    android:id="@id/animation_layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 AnimationLayout
 @Override
public void onFinishInflate() {
    super.onFinishInflate();
    mContent = (FrameLayout) findViewById(R.id.animation_layout_content);
    mSidebar = findViewById(R.id.animation_layout_sidebar);

    if (mSidebar == null) {
        throw new NullPointerException("no view id = animation_sidebar");
    }

    if (mContent == null) {
        throw new NullPointerException("no view id = animation_content");
    }

    mOpenListener = new OpenListener(mSidebar, mContent);
    mCloseListener = new CloseListener(mSidebar, mContent);
}
amukhachov
  • 5,822
  • 1
  • 41
  • 60
Taehyun Park
  • 1,352
  • 1
  • 9
  • 11
  • animation_layout_content has been declared in ids.xml so this should not be a problem(it wouldn't work on 4.1 either if I didn't declare this.) – Taehyun Park Sep 26 '12 at 23:11

1 Answers1

4

Change:

android:id="@id/animation_layout_content"

to:

android:id="@+id/animation_layout_content"

That is, unless you've added the ID elsewhere (in a resource xml file, or something). See this thread for details on @id vs @+id.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • Eric, animation_layout_content is in ids.xml – Taehyun Park Sep 26 '12 at 23:03
  • Is `animation_layout_sidebar` declared in the same way? – Cat Sep 26 '12 at 23:34
  • Yes. that's why I don't get this error. mSidebar has a returned view object but mContent has null. – Taehyun Park Sep 27 '12 at 22:14
  • Well.. hmm. Try swapping the two IDs (put the content in the sidebar and the sidebar in the main pane) and see if it won't read the sidebar ID. This should at least help you narrow down the source of the issue... – Cat Sep 27 '12 at 22:19
  • Eric, I fixed it. I just copied the menu_sidebar.xml instead of using include. And it works like a charm. It's really weird I am really suspicious that this is a bug because the above code worked on ICS and JellyBean – Taehyun Park Sep 28 '12 at 02:48
  • Interesting. I figured it had something to do with the `include`, but didn't think *removing* it would help. You may want to post the answer you discovered for others to find. – Cat Sep 28 '12 at 03:59
  • I just did it. Thanks for your comments and answer :) – Taehyun Park Sep 28 '12 at 22:14