[英]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. 当用户点击ClassOne.java中的星星时,会打开另一个活动,即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. 然后,用户键入复选并点击复选标记,该复选标记提交评论并将其带回ClassOne.java并在具有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 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 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 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'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
: 请检查ClassTwo.java
以下行:
TextView text = (TextView) inflater.inflate(R.layout.ClassOne, null);
Here layout xml file ClassOne.xml
has root as a ScrollView
. 这里布局xml文件ClassOne.xml
将root作为ScrollView
。 So when you will inflate that layout, you need to cast it as ScrollView
or View
parent of all views. 因此,当您要扩展该布局时,需要将其转换为ScrollView
或View
所有View
父级。
It is good practice to cast every inflated layout as View
and then get all different views using findViewById()
. 最好将每个膨胀的布局转换为View
,然后使用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. 您还可以使用Intent更改textView的文本,该文本是当前Activity中的另一个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:- 在你的调用活动的onCreate()中: -
Intent mIntent=getIntent();
if(mIntent!=null){
String msg=mIntent.getStringExtra("key");
textView.setText(msg);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.