0

I have a ListView where I until today have positioned two views: One ImageView to the left of its parent, and then I use a TextView positioned just to the right of this ImageView. In xml I use the following code to do that:

android:layout_toRightOf="@+id/single" , where single is an ImageView.

... so no problem whatsoever :-)

But today I came up with a new idea - that i want another ImageView in this ListView. I positioned this new view to the right side of its parent (ListView).

android:layout_alignParentRight="true"

Now a small problem arised when I did this: In cases where I have a long sentence in the TextView it overlappes this new ImageView. But according to the android documentation I found this code:

android:layout_toStartOf="@+id/volume" , where volume is the id for the new ImageView.

I really thought it would work. to the end of means Positions the end edge of this view to the start of the given anchor view ID. [reference]

So I thought everthing would work but when I compiled and started to test the app my app crashes when a listview i clicked. The crash is due to classcastexception. ImageView cannot be cast to TextView

So my question is why I get this ClassCastException? The only thing I do is to postion the end edge of the TextView to the start of the ImageView. Why do I get a classcastException when I use toStartOf? I do not get an exception when I position the TextView to the right of the other ImageView , using the xml-code android:layout_toRightOf="@+id/single"

In summary: my goal is to have three views in this ListView: two ImageView - one to the left and one to the right. And in between of these two ImageViews I want to place the TextView.

I get the exception in the getChildView-method.

<!-- Layout för undermenyerna -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="0px"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="0px"
android:background="#ffffff"
tools:context=".PhraseActivity" >

 <!-- view för ena ena könet, man eller kvinna --> 
  <TextView
    android:id="@+id/label_single"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_toRightOf="@+id/single"
    android:layout_toStartOf="@+id/volume"
    android:la

    />

    <!-- texview för båda könen, man och kvinna -->
  <TextView
    android:id="@+id/label_couple"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_toRightOf="@+id/couple"

    />

   <!-- texview för extra info -->
  <TextView
    android:id="@+id/extra_info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/couple"
    android:layout_marginTop="10sp"

    android:background="@drawable/back_extrainfo"
    android:padding="5sp"
    android:visibility="gone"
    android:textColor="#8c8c8c"
    />

  <!-- text länk till bild, tex karta som visar vart staden är i Thailand -->
  <TextView
    android:id="@+id/imagelink"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:focusableInTouchMode="true"
    android:text="Click to view on map"
    android:textColor="#3241d3"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/couple"
      />

  <!-- text länk till bild, samma syfte som texviewen ovan -->
  <TextView
    android:id="@+id/imagelink2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:text="Click to view image"
    android:focusableInTouchMode="true"
    android:textColor="#3241d3"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/extra_info"
      />

 <!-- view för en bild av ena könet, man eller kvinna --> 
 <ImageView
    android:id="@+id/single"
    android:layout_width="68sp"
    android:layout_height="68sp"

    />


<!-- view för en bild av båda könen, man och kvinna -->
<ImageView 
   android:id="@+id/couple"
   android:layout_width="100sp"
   android:layout_height="68sp"
   />

<ImageView
    android:id="@+id/volume"
    android:layout_width="100sp"
    android:layout_height="79sp"
    android:layout_alignParentRight="true"
/>

</RelativeLayout> 




  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      final ViewHolder holder;

      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.activity_subphrase, parent, false);
          holder = new ViewHolder();
          holder.text = (TextView) convertView.findViewById(R.id.label_single);
          holder.text2 = (TextView) convertView.findViewById(R.id.label_couple);
          holder.imageView = (ImageView) convertView.findViewById(R.id.single);
          holder.imageView2 = (ImageView) convertView.findViewById(R.id.couple);
          holder.volumeControl = (ImageView) convertView.findViewById(R.id.volume);
          convertView.setTag(holder);

      } else {  
          holder = (ViewHolder) convertView.getTag();
      }
      final int nChildren = getChildrenCount(groupPosition);


      final View v = convertView;
      switch (nChildren) {
        case 1:
            holder.imageView.setBackgroundResource(0);
            holder.imageView2.bringToFront();
            holder.imageView2.setVisibility(ImageView.VISIBLE);
            holder.text.setText(null);
            holder.text2.setText(contents[groupPosition][childPosition]);
            holder.imageView2.setBackgroundResource(R.drawable.man_woman_3);

            //extra(groupPosition, category, holder.text, convertView, parent);
            showThaiImages(groupPosition, holder.text, convertView, parent);
            // Väntar till all layout är avklarad - efter detta bearbetas animering. 

            vto = convertView.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);  //vet inte om denna metod är nödvändig
                holder.imageView2.bringToFront();
                holder.imageView2.setBackgroundResource(R.drawable.animation3);
                frameAnimation = (AnimationDrawable) holder.imageView2.getBackground();
                frameAnimation.start();
            }}); 
            break;

        case 2:


            try {
                System.out.println("ViewTreeObserver is alive = : " + vto.isAlive());
            } catch (Exception e) {
                e.printStackTrace();
            }

            //v.getViewTreeObserver().removeOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener());   //vet inte om denna metod är nödvändig
            holder.imageView2.setVisibility(View.GONE);
            holder.imageView2.setBackgroundResource(0);
            holder.imageView.bringToFront();
            holder.text2.setText(null);
            //holder.imageView.invalidate();
            holder.text.setText(contents[groupPosition][childPosition]);
            switch (childPosition) { // Switch-villkor för man eller kvinna. 
                case 0: // Man.
                    holder.imageView.setBackgroundResource(R.drawable.man_3);
                    break;
                case 1: // Kvinna.
                    holder.imageView.setBackgroundResource(R.drawable.woman_3);
                    break;
            }
            break;
      }

      holder.volumeControl.setBackgroundResource(R.drawable.speaker);
      notifyDataSetChanged();
      return convertView;
  }
Björn Hallström
  • 3,775
  • 9
  • 39
  • 50

1 Answers1

1

Delete the gen folder and clean the project and try again.This error comes wwhen you change the position of views.

Try this once.

sanky jain
  • 873
  • 1
  • 6
  • 14
  • I made a copy of the project and then it worked. The exception dissapeared but the code toStartOf did not work. Instead I used toLeftOf. – Björn Hallström Aug 28 '13 at 11:07