-3

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"/>
Fauzia Nito
  • 75
  • 1
  • 2
  • 7
  • 1
    Please share some code where you define/set `news_image`. Like this it looks like it might actually be a view ID, but not a real view. – Mauin Jan 30 '17 at 14:56
  • 1
    I have some *news* for you. You didn't show us what `news_image` is! – Chetan Kinger Jan 30 '17 at 14:57
  • `Cannot cast Int to android.view.View` simply mean that you are trying to cast a integer into a View, so `news_image` is a `int`. Google the excpetion first to understand what it means, than search the problem. – AxelH Jan 30 '17 at 14:58
  • 1
    You didn't show where you declared and initialize `news_image`, the XML only set the value of `R.id.news_image` – AxelH Jan 30 '17 at 15:02

1 Answers1

2

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"
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    Good guess. Hope it goes through. – Chetan Kinger Jan 30 '17 at 14:58
  • But remember am inside an Adapter class where my intent is , please can you try to edit my code with your answer – Fauzia Nito Jan 30 '17 at 15:01
  • @JordiCastilla, is a v an Int or View – Fauzia Nito Jan 30 '17 at 15:04
  • v is the view of the event: `public void onClick(View v) {` but is just an example, let me update the code – Jordi Castilla Jan 30 '17 at 15:05
  • Is it supposed to be Int v =(View) findViewById(R.id.news_image); or – Fauzia Nito Jan 30 '17 at 15:05
  • *Ok please update my code that will be better*, sorry, stackoverflow does not work in this way, you can edit your question to clarify, but not to change main theme of it, also I can't edit your code. Please look at my updated answer. – Jordi Castilla Jan 30 '17 at 15:08
  • Sorry i didnt mean editing my code , just wanted you to edit your answer according to my code – Fauzia Nito Jan 30 '17 at 15:17
  • `findViewById` cannot be resolved why ? is there any problem @JordiCastilla – Fauzia Nito Jan 30 '17 at 15:18
  • sure is some problem, but I have not enough information, look at the [**link**](https://developer.android.com/reference/android/view/View.html#findViewById(int)) I posted. As you can see is a `View` or `Activity` method and it states: *retrieve it with `View.findViewById()` or `Activity.findViewById()`*, that makes me ask..... **are you executing this in a View or Activity?**, please, **read complete and carefully my answer** and also link provided, **after this** if you have any doubts don't hesitate to ask, but show a bit of effort. – Jordi Castilla Jan 30 '17 at 15:22