我(显然)是Android和java编程的新手。 我有一个很长(并且很混乱)的脚本,并且已经将大部分内容排除在这个问题之外。 我有一个问题是:
ScrollView scroller = (ScrollView) findViewById(R.id.Scrollview); // Find Scrollview
scroller.removeAllViews();// Kill Scrollview's child from last time this ran (and I hope its children?)
LinearLayout scrollvert = new LinearLayout(this); // Make a new linear layout... WTF is "this"
scrollvert.setOrientation(LinearLayout.VERTICAL);// Make it a vertical layout
scroller.addView(scrollvert); // add this layout to Scrollview (scroller)
TextView[] tv=new TextView[epointer]; // Going to add some text views
for (int i = 0; i <= epointer; i++) { // does stuff that creates a stringbuilder called textLog
tv[i] = new TextView(this); // <--- Dies here!
tv[i].setText(textLog);
scroller.addView(tv[i]); }
我的xml已经有一个名为Scrollview的ScrollView。我想为此添加一个垂直的LinearLayout,这样我就可以在一个可滚动的视图中添加多个TextView(后来我希望能够点击特定的条目来修改或删除它们。)
到目前为止,我的结果是,当它到达...&#34;新的TextView&#34;部分。 我相信我的问题是我不理解&#34;背景。&#34;我在过去几周阅读了大量的帖子,他们都说要使用&#34;这&#34;,&#34; context&#34;,&#34; getContext&#34;或&#34; getApplicationContext& #34;但我真的不明白这是什么意思,以及它如何适用于我想要做的事情。
我做错了什么?我该如何解决?如果有更好的方法来做我想做的事情,我很乐意听到它,但我仍然想知道我在这里做错了什么。
感谢。
我已经添加了以下日志的相关部分:( MainActivity.java:199在哪里&#34;它死了&#34;)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.russdell.ytthourcalculator, PID: 3544
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22570)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22570)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.russdell.ytthourcalculator.MainActivity.calculate(MainActivity.java:199)
at com.russdell.ytthourcalculator.MainActivity.ME(MainActivity.java:83)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22570)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
您只能向scrollview添加一个子项。 所以进行此更改
ScrollView scroller = (ScrollView) findViewById(R.id.Scrollview); // Find Scrollview
scroller.removeAllViews();// Kill Scrollview's child from last time this ran (and I hope its children?)
LinearLayout scrollvert = new LinearLayout(this); // Make a new linear layout
scrollvert.setOrientation(LinearLayout.VERTICAL);// Make it a vertical layout
scroller.addView(scrollvert); // add this layout to Scrollview (scroller)
TextView[] tv=new TextView[epointer]; // Going to add some text views
for (int i = 0; i < epointer; i++) { //use this line to remove exception of indexoutofbound
tv[i] = new TextView(this);
tv[i].setText(textLog);
scrollvert.addView(tv[i]); //make this change. Adding to linear layout which is only one child of scrollview.
}
您要添加多个textview以在此处滚动查看scroller.addView(tv[i]);
Context是应用程序/对象当前状态的上下文。它是表示各种环境数据的实体。 Context帮助当前活动与外部android环境进行交互,如本地文件,数据库,与环境相关的类加载器,包括系统级服务在内的服务等。
参考this
答案 1 :(得分:0)
TextView[] tv=new TextView[epointer]; // Going to add some text views
for (int i = 0; i <= epointer; i++) { // total epointer+1 size????
它应该是i<epointer
它可以这样做
for (int i = 0; i < epointer; i++) {
TextView tv = new TextView(this); // will not die anywhere
tv.setText(textLog);
scrollvert.addView(tv); // these views should be added to linear layout not scroll view
其他方法可以直接将LinearLayout添加到xml中,并在添加所有这些文本视图之前将其清除。 希望这可以解决您的问题。