2017-03-01 13 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右のViewGroupを指していると、いつものようにあなたのターゲットウィジェットのためにそれに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はの一部であるとしてナビゲーションビューではない引き出し。

関連する問題