这个问题在这里已有答案:
尝试了将近一周的时间来在textview中实现一个简单的滚动条 . 我在这里问了几次,没有人生成工作代码 . 当我尝试这个时,我得到了一个NPE .
想要一个滚动条?然后我们将强制你编写XML,我们将强制你将它包装在布局中,我们强迫你使用scrollview,它仍然无法工作 . 保持简单怎么了?
将TextView放在ScrollView中https://developer.android.com/reference/android/widget/ScrollView.html xml <LinearLayout
android:id =“@ id / linearLayout”
机器人:layout_width = “match_parent”
机器人:layout_height = “match_parent”
android:orientation =“vertical”>
</的LinearLayout>
然后在活动中:LinearLayout lin =(LinearLayout)findViewById(R.id.linearLayout); TextView textView = new TextView(this); textView.setText(“你在地牢里 . ”); textView.setMaxLines(4); lin.addView(TextView的);
E / AndroidRuntime:FATAL EXCEPTION:main进程:dev.game.adventure,PID:12085 java.lang.RuntimeException:无法启动活动ComponentInfo {dev.game.adventure / dev.game.adventure.FullscreenActivity}:java.lang . NullPointerException:尝试在android.app.ActivityThread上的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)上的空对象引用上调用虚拟方法'void android.widget.LinearLayout.addView(android.view.View)' .handleLaunchActivity(ActivityThread.java:2892)在Android.app.Handler.dispatchMessage的android.app.ActivityThread.-wrap11(未知来源:0)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593) Handler.java:105)在android.app.Looper.loop(Looper.java:164)的android.app.ActivityThread.main(ActivityThread.java:6541)at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)引起b y:java.lang.NullPointerException:尝试在dev.game.adventure.FullscreenActivity.onCreate(FullscreenActivity.java)的空对象引用上调用虚方法'void android.widget.LinearLayout.addView(android.view.View)' . 157)在Android.app.A活动中的android.app.Asetrumentation.callActivityCnate(Activitymentation.java:1213)上的android.app.A.运行中的android.app.A活动 . android.app.Handler上android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)的android.app.ActivityThread.-wrap11(未知来源:0)app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) .dispatchMessage(Handler.java:105)位于android.app.Looper.loop(Looper.java:164)的android.app.ActivityThread.main(ActivityThread.java:6541),位于java.lang.reflect.Method.invoke(本地方法)在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 767)与目标VM断开连接,地址:'localhost:8785',transport:'socket'
然后我尝试以下来获得一个血腥的 IllegalStateException
,当我所要做的就是实现一个简单的滚动条到文本 .
this.setContentView(R.layout.activity_fullscreen);
LinearLayout lin = (LinearLayout) findViewById(R.id.linearLayout);
TextView textView = new TextView(this);
textView.setText("You are in a dungeon.");
textView.setMaxLines(4);
lin.addView(textView);
java.lang.RuntimeException:无法启动活动ComponentInfo {dev.game.adventure / dev.game.adventure.FullscreenActivity}:java.lang.IllegalStateException:指定的子级已有父级 . 您必须首先在孩子的父母上调用removeView() . 在Android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)android.app.A活动时,我在android.app.A活动时发现了android.app.A活动,发送活动(ActivityThread.java:2892) . ActivityThread $ H.handleMessage(ActivityThread.java:1593)位于android.app.AooT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:105)android.os.Looper.loop(Looper.java:164) (ActivityThread.java:6541)位于com.android.internal.os的com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)的java.lang.reflect.Method.invoke(Native Method) .ZygoteInit.main(ZygoteInit.java:767)引起:java.lang.IllegalStateException:指定的子节点已经有父节点 . 您必须首先在孩子的父母上调用removeView() . 在android.view.ViewGroup.addViewInner(ViewGroup.java:4915)
然后我尝试这个代码,自然它不起作用 . 我的Android游戏从简单的滚动条开始,我的textview溢出了游戏图像 . 为什么会有人希望此行为是默认的?为什么有人需要溢出作为默认行为?当它甚至无法呈现简单的滚动条时,我想要我的钱回到Android .
然后我试试这个 .
LinearLayout lin = new LinearLayout(this);
tbl.addView(tr);
TableRow tr2=new TableRow(this);
TextView textView = new TextView(this);
textView.setText("You are in a dungeon.");
textView.setMaxLines(4);
lin.addView(textView);
ScrollView sv = new ScrollView(this);
tr2.addView(sv);
TableRow.LayoutParams params = (TableRow.LayoutParams) sv.getLayoutParams();
params.span = 6; //amount of columns you will span
sv.setLayoutParams(params);
tbl.addView(tr2);
现在我可以证明它也不起作用 . 现在根本没有textview . 没有评论可以改变这一点 .
1 回答
我从中理解的是,您只是想使用这种复杂的解决方案使您的
TextView
可滚动,这在您的情况下无效 .您不需要在
TextView
中添加滚动来完成所有操作,只需参考this answer即可 .我的实施,
activity_main.xml
MainActivity.java
Screen Shot
希望这可以帮助 .