2014-07-18 6 views
0

내 응용 프로그램에는 많은 텍스트 뷰를 포함하는 매우 긴 텍스트 블록을 표시하는 데 사용되는 많은 텍스트 뷰가 포함 된 활동이 있습니다. 문제는 오리엔테이션 변경을 위해 텍스트의 스크롤 위치를 저장하고 응용 프로그램이 백그라운드로 들어갈 때 위치를 유지하려고 할 때 발생합니다. 그러나 화면의 방향을 변경하면 응용 프로그램이 충돌합니다.스크롤 위치를 저장하려고하면 응용 프로그램이 충돌합니다.

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/scrollChapter1"> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/Chapter1" 
      android:textSize="17sp" 
      android:padding="10px" 
      android:id="@+id/txtChapter1Title" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentA" 
      android:id="@+id/txtChapter1A" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1Title" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentB" 
      android:id="@+id/txtChapter1B" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1A" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentC" 
      android:id="@+id/txtChapter1C" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1B" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentD" 
      android:id="@+id/txtChapter1D" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1C" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentE" 
      android:id="@+id/txtChapter1E" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1D" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentF" 
      android:id="@+id/txtChapter1F" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1E" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:width="0dip" 
      android:textSize="16sp" 
      android:padding="10px" 
      android:text="@string/Chapter1ContentG" 
      android:id="@+id/txtChapter1G" 
      android:maxLength="9000" 
      android:layout_below="@+id/txtChapter1F" 
      /> 
</RelativeLayout> 
</ScrollView> 

Chapter1.java

Chapter1.xml : 아래

코드입니다

public class chapter1 extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chapter1); 
    WindowManager w = getWindowManager(); 
    Display d = w.getDefaultDisplay(); 
    if(d.getWidth() > d.getHeight()){ 
     Log.d("Orientation", "Landscape"); 
    }else{ 
     Log.d("Orientation", "Potrait"); 
    } 
} 
@Override 
protected void onSaveInstanceState(Bundle outState){ 
    super.onSaveInstanceState(outState); 
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollChapter1); 
    final TextView textView = (TextView) scrollView.getChildAt(0); //Crashes at this line 
    final int firstVisibleLineOffset = textView.getLayout().getLineForVertical(scrollView.getScrollY()); 
    final int firstVisibleCharacterOffset = textView.getLayout().getLineStart(firstVisibleLineOffset); 
    outState.putInt("ScrollViewContainerTextViewFirstVisibleCharacterOffset", firstVisibleCharacterOffset); 
} 
@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState){ 
    super.onRestoreInstanceState(savedInstanceState); 
    final int firstVisibleCharacterOffset = savedInstanceState.getInt("ScrollViewContainerTextViewFirstVisibleCharacterOffset"); 
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollChapter1); 
    scrollView.post(new Runnable() { 
     @Override 
     public void run() { 
      final TextView textView = (TextView) scrollView.getChildAt(0); 
      final int firstVisibleLineOffset = textView.getLayout().getLineStart(firstVisibleCharacterOffset); 
      final int pixelOffset = textView.getLayout().getLineTop(firstVisibleLineOffset); 
      scrollView.scrollTo(0, pixelOffset); 
     } 
    }); 
} 

}

로그 캣 :

07-18 15:52:39.921  806-806/com.example.ZindaRud D/Orientation﹕ Potrait 
07-18 15:52:41.800  806-810/com.example.ZindaRud D/dalvikvm﹕ GC_CONCURRENT freed 155K, 4% free 7169K/7431K, paused 6ms+6ms 
07-18 15:52:41.809  806-806/com.example.ZindaRud D/Orientation﹕ Potrait 
07-18 15:52:44.500  806-806/com.example.ZindaRud W/TextLayoutCache﹕ computeValuesWithHarfbuzz -- need to force to single run 
07-18 15:52:48.760  806-806/com.example.ZindaRud D/AndroidRuntime﹕ Shutting down VM 
07-18 15:52:48.760  806-806/com.example.ZindaRud W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8) 
07-18 15:52:48.809  806-806/com.example.ZindaRud E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 
     at com.example.ZindaRud.chapter1.onSaveInstanceState(chapter1.java:31) 
     at android.app.Activity.performSaveInstanceState(Activity.java:1113) 
     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185) 
     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3321) 
     at android.app.ActivityThread.access$700(ActivityThread.java:122) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4340) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
     at dalvik.system.NativeStart.main(Native Method) 

고마워 녀석들은 ClassCastException을 수정했지만 방향 변경시 스크롤 위치를 저장하고 유지하지 않는 것 같습니다.

+0

ecllipse를 닫고 다시 시작하십시오. my xml을 편집 한 후에 클래스 캐스트 예외가 발생합니다. 때로는 –

답변

2

는 0에서 라인

final TextView textView = (TextView) scrollView.getChildAt(0);

있는 ScrollView의 직계 자식

에서 텍스트 뷰에 RelativeLayout의 캐스팅 때문에 당신은 캐스트 예외를 얻고 명백한된다은 레이아웃에서 볼 수 있듯이 RelativeLayout의입니다.

0

실수로 죄송합니다. micro.pavo @

final RelativeLayout rLayout = (RelativeLayout) scrollView.getChildAt(0); 
final TextView textView = (TextView) rLayout.getChildAt(0); 

을 사예드 또는 직접

final TextView textView = (TextView)findViewById(R.id.txtChapter1Title); 
+0

네 그렇지만 앱 크래시가 발생했습니다 – Faizan

+0

하나의 커다란 텍스트 블록을 구성하는 5 개 이상의 textview가 있습니다.이 코드는 첫 번째 textview'textView = textView = (TextView) rLayout.getChildAt (0);' – Faizan

+0

예 @Faizan 나머지는 다음과 같이 할 수 있습니다. 최종 TextView textView = (TextView) rLayout.getChildAt (0); 최종 TextView textView1 = (TextView) rLayout.getChildAt (1); 등등 – PedroAGSantos

0

최종 텍스트 뷰 텍스트 뷰 = (텍스트 뷰) scrollView.getChildAt를 액세스 할 수있는 재사용 (0); 이 라인

RelativeLayout에서 // 충돌은 ScrollView의 자식이고 당신은 TextView에 캐스팅된다.

그래서,

java.lang.ClassCastException가 : android.widget.RelativeLayout이 캐스트가 android.widget 할 수 없습니다.당신이 xml 코드에서 볼 수 있듯이이 줄

final TextView textView = (TextView) scrollView.getChildAt(0); 

TextViewRelativeLayout를 캐스팅하려고하기 때문에 텍스트 뷰

0

당신은 ClassCastException을 받고, ScrollView에서 첫 번째 자식이 RelativeLayout입니다, 그것을 만드는 시도 다음과 같이하십시오 :

final RelativeLayout rLayout = (RelativeLayout) scrollView.getChildAt(0); 
관련 문제