我試圖創建一個能夠滾動大量文本的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);
}
}
如果多行TextView包含的文本比它能顯示的多,它將會滾動。 – Squonk 2012-02-10 20:32:05
發佈logcat錯誤消息 – kosa 2012-02-10 20:33:26
LogCat強制關閉時會說什麼? – CaseyB 2012-02-10 20:33:54