2

I'd like to use the ImageViewTopCrop class described in this question:

ImageView scaling TOP_CROP

My custom view is based on this code:

Custom Image View class

Here is a layout file using this custom view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_background"
android:background="#000"
android:orientation="vertical">
<FrameLayout
    android:id="@+id/details_frame"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="6">

<com.grayraven.imagetest.ImageViewTopCrop
    android:id="@+id/grid_item_image"
    android:src="@drawable/hobbit"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     />

     </FrameLayout>

Here's how I'm trying to display it in code:


com.grayraven.imagetest;

import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.text.Html; import android.widget.ImageView; import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class DetailsActivity extends ActionBarActivity { private TextView titleTextView; private com.grayraven.imagetest.ImageViewTopCrop imageView;

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

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

    String title = getIntent().getStringExtra("title");
    String imageUrl = getIntent().getStringExtra("imageUrl");
    titleTextView = (TextView) findViewById(R.id.title);
    imageView = (com.grayraven.project1.ImageViewTopCrop) findViewById(R.id.grid_item_image);  

//Exception thrown here:
//android.widget.ImageView cannot be cast to com.grayraven.imagetest.ImageViewTopCrop

//can't go any further
}

}

What simple thing am I missing?

Community
  • 1
  • 1
Jim In Texas
  • 1,524
  • 4
  • 20
  • 31
  • `com.grayraven.imagetest.ImageViewTopCrop` is not equals with `com.grayraven.project1.ImageViewTopCrop` you declare first path for your image but cast your widget with second path. is that right? – Shayan Pourvatan Jul 30 '15 at 18:58

2 Answers2

0

I think you are type casting with wrong class.

You used

com.grayraven.imagetest.ImageViewTopCrop

in your xml but type casting with com.grayraven.project1.ImageViewTopCrop

Prasad
  • 3,462
  • 1
  • 23
  • 28
  • That was a typo, when this didn't work in project1 I made a test project called imagetest. Even when the paths are correct I get the exception. :( – Jim In Texas Jul 30 '15 at 20:19
0

I was missing a constructor in my custom view class. Never mind.

Jim In Texas
  • 1,524
  • 4
  • 20
  • 31