2016-12-09 7 views
0

ClassOne.javaの星をクリックすると、ClassTwo.javaという別のアクティビティが開きます。次に、ユーザのタイプがレビュー&のチェックマークをクリックすると、レビュー&がClassOne.javaに戻されます。&は、TextViewを持つレビューの下に書面によるレビューを表示します。java.lang.ClassCastException:android.widget.ScrollViewはandroid.widget.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> 

<?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> 
+0

使用この 'ビュービュー= inflater.inflateを(R.layout.ClassOne、ヌル) ; –

+0

'View text = inflater.inflate(R.layout.ClassOne、null);' – sushildlh

+1

@sushildlhキャストする必要はありません。 'return'型はすでに' View'です。 –

答えて

0

があなたのClassTwo.javaに線の下に確認してくださいClassTwo.xml:

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

レイアウトxmlファイルClassOne.xmlは、ルートがScrollViewです。だから、そのレイアウトを膨らませるときは、すべてのビューの親としてScrollViewまたはViewの親としてキャストする必要があります。

膨張したすべてのレイアウトをViewとしてキャストし、findViewById()を使用してすべての異なるビューを取得することをお勧めします。

だからあなたの行は以下のように変更します。

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

おかげ

0

をあなたはまた、現在の活動から別の活動であるのTextViewのテキストを変更する意図を使用することができます。現在の活動に

: - のonCreateで

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

()あなたの呼び出し元の活動の: -

Intent mIntent=getIntent(); 
     if(mIntent!=null){ 
      String msg=mIntent.getStringExtra("key"); 
      textView.setText(msg); 
     } 
関連する問題