简体   繁体   中英

Cannot cast Int to android.view.View using Transition in Adapter class

Am trying to use transitions in my App but am facing this problem,

Cannot cast Int to android.view.View

Below is my code:

holder.newsRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newsIntent = new Intent(NewsAdapter.this.c, NewsDetails.class);

                newsIntent.putExtra("newstitle",NewsTitle);
                newsIntent.putExtra("detailednews",DetailedNews);
                newsIntent.putExtra("newsdate",NewsDate);
                newsIntent.putExtra("newsphoto",NewsPhoto);


                ActivityOptionsCompat options= ActivityOptionsCompat.makeSceneTransitionAnimation(this,(View)news_image,"image_transit");

                c.startActivity(newsIntent,options.toBundle());

            }
        });

Am trying to integrate it in the Adapter class , but this is the line where the problem is :

`ActivityOptionsCompat options= ActivityOptionsCompat.makeSceneTransitionAnimation(this,(View)news_image,"image_transit")`;

** problem is found on these words**

(View)news_image

My xml code of the ImageView.

 <ImageView

                android:id="@+id/news_image"
                android:layout_width="80dp"
                android:transitionName="image_transit"
                android:layout_height="80dp"
                android:src="@drawable/bbb"/>

PROBLEM: in android, you store the id's of your objects as int, but the int is the view id, not the view itself .

SOLUTION: in order to work with it you need to get the view use findViewById .

ImageView yourImageView = (ImageView) findViewById(R.id.news_image);

In your case you can also use it directly in the problematic method:

ActivityOptionsCompat options= 
      ActivityOptionsCompat.makeSceneTransitionAnimation(this,  
                                                         findViewById(R.id.news_image),
                                                         "image_transit")`;

Because you placed it in the xml layout:

<ImageView android:id="@+id/news_image"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM