2017-01-23 5 views
0

私の計画は、ユーザーが行に触れるとRecyclerViewアダプタのImageViewからベクトルの色を変更することです。私はちょうどタッチされた行から色を変更したい。私はこの仕事を行う必要があり、コードを書いてきましたが、私はエラーを取得しています:タッチ時にImageView描画可能ベクトルの色を変更する

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference 

私は多くのことを試してみたが、エラーが常に問題となっています。

これは私のRecyclerView行xmlです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_height="match_parent" 
 
    android:layout_width="match_parent" 
 
    android:focusable="true" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:clickable="true" 
 
    android:background="?android:attr/selectableItemBackground" 
 
    android:orientation="vertical"> 
 

 
    <!-- Setting icon --> 
 
    <ImageView 
 
     android:id="@+id/settingImage" 
 
     android:layout_width="24dp" 
 
     android:layout_height="24dp" 
 
     android:layout_centerVertical="true" 
 
     android:layout_alignParentStart="true" 
 
     android:layout_marginRight="8dp" 
 
     android:layout_marginTop="1dp" 
 
     android:layout_marginBottom="1dp" 
 
     android:contentDescription="Icon" 
 
     android:src="@mipmap/ic_launcher" /> 
 

 
    <!-- Setting title --> 
 
    <TextView 
 
     android:id="@+id/settingTitle" 
 
     android:textColor="@color/colorBlack" 
 
     android:layout_width="match_parent" 
 
     android:layout_marginLeft="40dp" 
 
     android:layout_marginTop="14dp" 
 
     android:textSize="16dp" 
 
     android:layout_height="wrap_content" /> 
 

 
    <!-- Setting subtitle --> 
 
    <TextView 
 
     android:id="@+id/settingSubtitle" 
 
     android:layout_below="@id/settingTitle" 
 
     android:layout_marginBottom="14dp" 
 
     android:layout_marginLeft="40dp" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" /> 
 

 
</RelativeLayout>

これは私が塗りつぶしの色を変更したい私のベクタ形式です:

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:width="24dp" 
 
    android:height="24dp" 
 
    android:viewportHeight="24.0" 
 
    android:viewportWidth="24.0"> 
 
    <path 
 
     android:fillColor="@color/iconGray" 
 
     android:pathData="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z" /> 
 
</vector>

これは私がクリックしたときに色を変更したい私のアダプタです:

// Get setting holder type 
 
holder.type = setting.getType(); 
 

 
// OnTouchListener for holder/vector color change 
 
holder.itemView.setOnTouchListener(new View.OnTouchListener() { 
 
    @Override 
 
    public boolean onTouch(View v, MotionEvent event) { 
 
     // Define setting holder 
 
     MySettingHolder holder = (MySettingHolder) (v.getTag()); 
 

 
     // Set view for getting settingImage 
 
     View view = (View)v.getParent(); 
 

 
     // ImageView for changing color 
 
     ImageView imageView = (ImageView) v.findViewById(R.id.settingImage); 
 

 
     // Define switch for line 
 
     switch (holder.type) { 
 
      // Case 1 = Logout 
 
      case 1: 
 
       // Change ImageView color 
 
       DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.colorPrimary)); 
 
       break; 
 

 
      case 2: 
 

 
       break; 
 

 
      default: 
 

 
       break; 
 
     } 
 

 
     return false; 
 
    } 
 
});

+0

はそれを手に入れました!後で回答を投稿する – ItsOdi1

答えて

0

ソリューションは、コンテキストを変更しました:

旧:

DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.colorPrimary));

新:

DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.iconGray));

関連する問題