![](/img/trans.png)
[英]java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
[英]java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView
當用戶點擊ClassOne.java中的星星時,會打開另一個活動,即ClassTwo.java。 然后,用戶鍵入復選並點擊復選標記,該復選標記提交評論並將其帶回ClassOne.java並在具有TextView的“評論”下顯示他們的書面評論。
當我點擊檢查提交我不斷收到錯誤。 我該如何解決這個錯誤?
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'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>
請檢查ClassTwo.java
以下行:
TextView text = (TextView) inflater.inflate(R.layout.ClassOne, null);
這里布局xml文件ClassOne.xml
將root作為ScrollView
。 因此,當您要擴展該布局時,需要將其轉換為ScrollView
或View
所有View
父級。
最好將每個膨脹的布局轉換為View
,然后使用findViewById()
獲取所有不同的視圖。
所以改變你的行如下:
View rootView = inflater.inflate(R.layout.ClassOne, null);
謝謝
您還可以使用Intent更改textView的文本,該文本是當前Activity中的另一個Activity。
在當前活動中: -
Intent intent= new Intent(CurrentActivity.this,CallingActivity.class);
intent.putExtra("key","Review changed from a different activity");
startActivity(intent);
finish();
在你的調用活動的onCreate()中: -
Intent mIntent=getIntent();
if(mIntent!=null){
String msg=mIntent.getStringExtra("key");
textView.setText(msg);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.