首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findViewById返回null

findViewById返回null
EN

Stack Overflow用户
提问于 2012-09-25 03:46:28
回答 1查看 1.8K关注 0票数 1

我正在构建一个使用像Facebook这样的滑动菜单的安卓应用程序,但是ViewGroup不能正常工作。它在我运行4.1的手机上运行得很好,但是当我试图在运行2.3的模拟器上运行我的代码时,mContent = (FrameLayout) findViewById(R.id.animation_layout_content);这一行总是返回null;然而,mSidebar正确地返回了视图。如果有人能帮我解决这个问题,我将不胜感激。谢谢!

代码语言:javascript
复制
    @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"/>

代码语言:javascript
复制
 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);
}
EN

回答 1

Stack Overflow用户

发布于 2012-09-25 03:50:30

更改:

代码语言:javascript
复制
android:id="@id/animation_layout_content"

至:

代码语言:javascript
复制
android:id="@+id/animation_layout_content"

也就是说,除非您已经在其他地方(在资源xml文件或其他文件中)添加了ID。有关@id@+id的详细信息,请参阅this thread

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12571844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档