ScrollView中的Android TextView无法正常工作 - 强制关闭

时间:2012-02-10 20:24:21

标签: android textview scrollview forceclose

我正在尝试创建一个能够滚动大量文本的文本视图。我将它嵌套在一个scrollview中,当我试图测试它时,我会在手机和平​​板电脑上关闭它。当我没有滚动视图运行时,它可以工作,但显然不会滚动。以下是我在XML文件中的布局:

<?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。这将获取主视图上列表中触摸的项目,并在textview中显示更多信息。

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);
}
}

1 个答案:

答案 0 :(得分: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);这是错的。

您已将其父级设置为内容视图,将其删除即可。