1

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) 
Russ
  • 13
  • 4
  • "Dies here!" is not really an adequate description of the problem. If it's crashing, look at the stack trace. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors I would point out that an array with _n_ items will have a maximum index of _n-1_, so your `for` loop is going one too far. – Mike M. Feb 07 '17 at 03:41
  • Use the "Logcat" -- it will tell you what the problem is. https://www.youtube.com/watch?v=QUkS6CfzK9M --- https://www.youtube.com/watch?v=QqjrR11tuz0 – Tasos Feb 07 '17 at 04:03
  • I dont see anything that `dies here!`. You should post your log. – Kingfisher Phuoc Feb 07 '17 at 04:05
  • `epointer` has value 0. You're creating a zero-length array, and trying to assign something to the first slot, which it doesn't have. – Mike M. Feb 08 '17 at 00:25
  • Thanks, Mike M., I completely missed that. – Russ Feb 10 '17 at 00:23

2 Answers2

0

You can add only one child to scrollview. So make this changes

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.
            }

You are adding multiple textview to scroll view here scroller.addView(tv[i]);

Context

Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.

Refer this

Community
  • 1
  • 1
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0
TextView[] tv=new TextView[epointer]; // Going to add some text views

 for (int i = 0; i <= epointer; i++) {   // total epointer+1  size???? 

It should be i<epointer It can be done like this

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 

Other ways could be directly adding LinearLayout into xml , and clearing it before adding all these text views. Hope this resolves your issue.

Renu Yadav
  • 316
  • 1
  • 6