2012-02-10 35 views
2

我試圖創建一個能夠滾動大量文本的textview。我將它嵌套在scrollview中,當試圖測試它時,我的手機和平板電腦上的力會關閉。當我運行沒有滾動視圖,它的工作原理,但顯然不滾動。以下是我在爲我的佈局XML文件:ScrollView中的Android TextView不工作 - 強制關閉

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:scrollbars="horizontal" android:isScrollContainer="true">  
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tutView"> 

    </TextView> 
</LinearLayout> 
</ScrollView> 

編輯:logcat的信息:

02-10 14:51:44.402: W/dalvikvm(5948): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8) 
02-10 14:51:44.414: E/AndroidRuntime(5948): FATAL EXCEPTION: main 
02-10 14:51:44.414: E/AndroidRuntime(5948): java.lang.RuntimeException: Unable to start activity ComponentInfo{rg.ah/rg.ah.TutViewerActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.os.Looper.loop(Looper.java:137) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at dalvik.system.NativeStart.main(Native Method) 
02-10 14:51:44.414: E/AndroidRuntime(5948): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3337) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.view.ViewGroup.addView(ViewGroup.java:3208) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.view.ViewGroup.addView(ViewGroup.java:3188) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.Activity.setContentView(Activity.java:1855) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at rg.ah.TutViewerActivity.onCreate(TutViewerActivity.java:25) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.Activity.performCreate(Activity.java:4465) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
02-10 14:51:44.414: E/AndroidRuntime(5948):  ... 11 more 
02-10 14:51:44.496: D/dalvikvm(5948): GC_CONCURRENT freed 192K, 4% free 9285K/9607K, paused 2ms+9ms 

編輯2:增加了我的onCreate有問題的錯誤。這需要在主視圖上的列表中觸及的項目,並在文本視圖中顯示更多信息。

public class TutViewerActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.textview); 

    Intent launchingIntent = getIntent(); 
    String content = launchingIntent.getData().toString(); 

    TextView viewer = (TextView) findViewById(R.id.tutView); 
    viewer.setMovementMethod(new ScrollingMovementMethod()); 
    viewer.setText(Html.fromHtml(content)); 

    setContentView(viewer); 
} 
} 
+0

如果多行TextView包含的文本比它能顯示的多,它將會滾動。 – Squonk 2012-02-10 20:32:05

+1

發佈logcat錯誤消息 – kosa 2012-02-10 20:33:26

+1

LogCat強制關閉時會說什麼? – CaseyB 2012-02-10 20:33:54

回答

1

例外是告訴你IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

這意味着您嘗試在另一個視圖中放置一個父視圖。

確保你沒有setContentView(R.id.tutView)或類似的東西。

如果您的佈局xml的名稱是main.xml,那麼正確的做法是setContentView(R.layout.main)

編輯

你爲什麼寫這條線? setContentView(viewer);這是什麼錯誤。

你已經將它的父母設置爲內容視圖,刪除它,你就沒事。

+0

@Rekar是的錯誤是在onCreate而不是XML。它甚至告訴你'在rg.ah.TutViewerActivity.onCreate(TutViewerActivity.java:25'所以第25行。我與Rotemmiz這可能是你的setContentView() – Blundell 2012-02-11 00:43:28

+0

Sry它花了我很長時間。我添加了未經編輯的代碼讓我們看看,我所做的不是將textview放入滾動視圖中,而是將其保存在頂層,並添加了'setMovementMethod'代碼以允許文本作爲臨時修訂滾動。我仍然非常感興趣修復我以前的代碼。 – Rekar 2012-02-11 02:44:50