简体   繁体   中英

java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView

When user clicks on stars in ClassOne.java another activity opens which is ClassTwo.java. Then user types in review & clicks on the check mark which submits the review & takes them back to ClassOne.java & displays their written review under "Reviews" which has a TextView.

When i click on the check to submit i keep getting the error. How can i fix this error ?

ClassOne.java Image ClassTwo.java Image

ClassTwo.java    

public ImageView sumbitCheck;
public Textview reviewDisplay;

submitCheck.setOnClickListener(new View.OnClickListener() { //submit check image
        @Override
        public void onClick(View v) {

            LayoutInflater inflater = getLayoutInflater(); //TextView inside ClassOne.java
            TextView text = (TextView) inflater.inflate(R.layout.ClassOne, null);
            reviewDisplay = (TextView) text.findViewById(R.id.display_review);


            Intent intent = new Intent(ClassTwo.this, ClassOne.class);
            startActivity(intent);
        }
    });

ClassOne.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll_view">

        <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@+id/relativeLayout5"
          android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/display_review" />
        </RelativeLayout>

<ScrollView>

ClassTwo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:background="#fff">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#257985"
    android:layout_alignParentTop="true"
    android:id="@+id/relativeLayout6"
    android:paddingBottom="10dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/back_arrow"
        android:src="@drawable/arrow"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Chipotle Chicken Fajitas"
        android:id="@+id/textView22"
        android:textSize="22sp"
        android:textColor="#fff"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp" />

    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:id="@+id/submit_check"
        android:src="@drawable/submitarrow"
        android:layout_marginTop="11dp"
        android:layout_marginLeft="20dp" />

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/relativeLayout6"
    android:layout_centerHorizontal="true"
    android:id="@+id/relativeLayout7">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="You&apos;re Review"
        android:id="@+id/textView21"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:textSize="20sp" />

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:scaleX=".5"
        android:scaleY=".4"
        android:layout_alignParentBottom="true" />

   </RelativeLayout>

    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relativeLayout7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:hint="What did you think?"
    android:gravity="top"
    android:background="#e8e5e5"
    android:id="@+id/users_review" />


    </RelativeLayout>

Please check below line in your ClassTwo.java :

 TextView text = (TextView) inflater.inflate(R.layout.ClassOne, null);

Here layout xml file ClassOne.xml has root as a ScrollView . So when you will inflate that layout, you need to cast it as ScrollView or View parent of all views.

It is good practice to cast every inflated layout as View and then get all different views using findViewById() .

So change your line as below:

 View rootView = inflater.inflate(R.layout.ClassOne, null);

Thanks

You can also use Intent to change text of a textView which is of another Activity from current Activity.

In current Activity:-

Intent intent= new Intent(CurrentActivity.this,CallingActivity.class);
                intent.putExtra("key","Review changed from a different activity");
                startActivity(intent);
                finish();

In onCreate() of your calling Activity:-

Intent mIntent=getIntent();
        if(mIntent!=null){
            String msg=mIntent.getStringExtra("key");
            textView.setText(msg);
        }

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