简体   繁体   English

findViewById返回null

[英]findViewById returns null

I am building an Android app using a sliding menu like Facebook but the ViewGroup doesn't work properly. 我正在使用像Facebook这样的滑动菜单构建Android应用程序,但ViewGroup无法正常工作。 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); 它在运行4.1的手机上工作正常,但是当我试图在运行2.3的模拟器上运行我的代码时, mContent = (FrameLayout) findViewById(R.id.animation_layout_content); this line always returns null ; 这一行总是返回null ; however, mSidebar returns the view correctly. 但是, mSidebar正确返回视图。 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);
}

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). 也就是说,除非您已将ID添加到其他位置(在资源xml文件中,或其他内容)。 See this thread for details on @id vs @+id . 有关@id vs @+id详细信息,请参阅此主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM