2017-03-01 4 views
0

ImageView의 비트 맵을 변경하려고했으나 코드에서 null 객체를 계속 반환합니다.findViewById가 AppCompatActivity에서 null을 반환합니다.

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

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    // Drawer Options 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    // Drawer Header 
    ImageView img = (ImageView) findViewById(R.id.header_imageView); 
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.marius_cropped_48x48); 
    RoundedBitmap profile_pic = new RoundedBitmap(bm); 
    if (img != null) { 
     img.setImageBitmap(profile_pic.getRoundedBitmap()); 
     Log.d(TAG, "ImageView OK"); 
    } else { 
     Log.d(TAG, "ImageView null"); 
    } 

} 

여기서 "img"는 항상 null입니다. 나는 이것을 행운으로 바꾸려고 노력했다.

ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView); 

또는

LinearLayout header = (LinearLayout) drawer.findViewById(R.id.header_linearLayout); 
    ImageView img = (ImageView) header.findViewById(R.id.header_imageView); 

하지만 행운

. 다음은 내 XML 파일입니다.

activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_drawer" 
     app:menu="@menu/activity_drawer_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

그리고 nav_header_drawer.xml 파일

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/header_linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/header_imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" /> 

    <TextView 
     android:id="@+id/header_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="text" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/header_company" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="as" /> 

</LinearLayout> 

답변

1

successfuly 탐색 서랍의 헤더에 위젯을 찾으려면, 당신은에 getHeaderView()를 호출하여 먼저 올바른 컨테이너보기를 얻을 필요 그것 :

View header = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0); 

그리고 나서 header 오른쪽 뷰 그룹을 가리키는 것은 평소와 같이 대상 위젯 위해에 findViewById()를 호출 할 수

ImageView iv = ((ImageView) header.findViewById(R.id.header_imageView)); 
+0

대단히 감사합니다. Marcin !!!! 나는 내 문제를 해결했다. – Nagas

+1

@Nagas 도움이 필요하면 답변을 수락하고, 답변 옆에있는 눈금을 클릭하십시오. – Redman

0

변경 다음 줄에서

ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView); 

ImageView img = (ImageView) navigationView.findViewById(R.id.header_imageView); 

에 header_imageView이 부분이므로 탐색보기가 아닌 서랍.