I am (obviously) new to Android and java programming. I have a pretty long (and messy) script and have left most of it out of this question. What I have a question about is this:
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]); }
I already have a ScrollView named Scrollview from my xml. I want to add a vertical LinearLayout to this so I can then add multiple TextViews in a scrollable view (later I want to be able to click on particular entries to modify or delete them).
My result so far is that it dies when it gets to the ..."new TextView" part. I believe my problem is that I do not understand "context." I have read a ton of posts over the past weeks and they all say to use "this", "context", "getContext", or "getApplicationContext" but I really don't understand what that really means and how it applies to what I am trying to do.
What am I doing wrong? And how can I fix it? If there is a better way to do what I am trying to do, I'd love to hear it but I still want to understand what I am doing wrong here.
Thanks.
I have added the relevant part of the log below: (MainActivity.java:199 is where "It dies")
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)