2014-12-14 3 views
9

내 레이아웃에서 내 Toolbar을 찾지 못하는 이유는 무엇입니까?툴바 - null을 반환하는 findViewbyID

setContentView(R.layout.activity_main); 

toolbar = (Toolbar) findViewById(R.id.toolbar); 
if (toolbar != null) { 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeButtonEnabled(true); 
} 

그 후, 툴바는 여전히 null입니다.

activity_man.xml :

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    tools.context=".PlayerActivity"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar"/> 
    <ImageView 
     android:id="@+id/background_image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop"/> 
     : (goes on) 

편집 :

toolbar.xml :

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary"/> 

감사합니다!

+2

은 무엇입니까 사용하여 작업 표시 줄을

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="windowActionBar">false</item> </style> 

전면에 도구 모음을 가져와을 제거 styles.xml에서 다음 'res/layout/toolbar.xml'에 있습니까? 'R.id.toolbar'가'Toolbar'가 아니라''을 가리키고 있습니다. "내 툴바 레이아웃에는 툴바 만 있습니다"-하지만이 레이아웃은 질문의 일부가 아니기 때문에 내용을 볼 수 없습니다. – CommonsWare

+0

toolbar.xml에'setContentView'를 사용하고 있습니까? –

+0

setcontenview 및 도구 모음 레이아웃으로 업데이트되었습니다. 조금 놀았지만 이제는 작동하지만 툴바가 화면에 보이지 않습니다. –

답변

4

코드는 잘 당신이 비활성화 기본 액션 바 없을 수 있습니다 노력하고 있습니다 :

변경을 toolbar.bringToFront();

+0

이미 삭제되었습니다. 나는 @ + id/background_image ImageView가 툴바가 보이지 않게하는 것 같지만 그 문제를 해결하는 방법을 모른다. –

+0

이 메서드를 사용하십시오 toolbar.bringToFront(); –

+0

아아아, 네! 고마워요! –

관련 문제