1. Download our Official Android App: Forums for Android!

.setText in other class causes crash

Discussion in 'Android Apps & Games' started by Ragoune, Feb 18, 2011.

  1. Ragoune

    Ragoune Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Feb 18, 2011

    Feb 18, 2011
    4
    0
    5
    Hey,

    I've been working on this bug for a very long time and I am now on a stage where I need some help as I'm a beginner in Java.

    I have a small application which fetches source code from a site. This is done via a JavaScript Interface (I've found out that this is the only way to get the source code). Afterwards it has to put the retreived code into a TextView.

    The problem is, that when I call .setText at the 2D array in which I stored the TextView, the app crashes. Code explains more so here's a simplified example of my code so far:
    Code (Text):
    1.  
    2. public class Moro extends Activity
    3.     {
    4.     ...
    5.     TextView timetable_class[][] = new TextView[9][5];
    6.     ...
    7.     public void onCreate(Bundle savedInstanceState)
    8.             {
    9.         int id_start = 15;
    10.             for (int i = 0; i < 9; i ++) //Row
    11.                 {
    12.                 for (int i2 = 0; i2 < 5; i2 ++) //Column
    13.                     {
    14.                     //Create textviews
    15.                     timetable_class[i][i2] = new TextView(app);
    16.                     timetable_class[i][i2].setId(id_start);
    17.                    
    18.                     layout_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    19.                     ...
    20.                     layout.addView(timetable_class[i][i2], layout_params);
    21.                    
    22.                     id_start += 1;
    23.                     }
    24.                 }
    25.         }
    26.  
    27.     class MyJavaScriptInterface
    28.             {
    29.             @SuppressWarnings("unused")
    30.             public void returnHTML(String html)
    31.                 {
    32.                 int id_start = 15;
    33.                
    34.                 int start = html.indexOf("<td>")+1;
    35.                 for (int i = 0; i < 1; i ++) //Row
    36.                     {
    37.                     for (int i2 = 0; i2 < 1; i2 ++) //Column
    38.                         {
    39.                         timetable = html.substring(html.indexOf("<td", start), html.indexOf("</td>", start))
    40.                             .replace("<td>", "")
    41.                             .replace("<td class=\"vrij\">", "")
    42.                             .replace("<span class=\"nobr\">", "")
    43.                             .replace("</span>", "")
    44.                             .replace("&nbsp;", "")
    45.                             .trim()
    46.                             .replace(" W", " ");
    47.                    
    48.                         start = html.indexOf("</td>", start)+1;
    49.                    
    50.                         timetable_class[i][i2].setText(timetable);
    51.                         }
    52.                     }
    53.                 }
    54.             }
    55.  
    56. public void setTimetable()
    57.         {
    58.     ...
    59.     web.loadUrl("javascript:window.HTMLOUT.returnHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    60.     ...
    61.         }
    62.  
    As you can see, a .setText() is executed in the MyJavaScriptInterface. This one creates a crash and stops the app. I know that 'timetable' has a value because I used Toasts to show its value.

    I hope one of you can help me out.

    Ragoune
     

    Advertisement

  2. idavis

    idavis Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    Feb 11, 2011

    Feb 11, 2011
    27
    4
    16
    Ragoune,
    Could it be that it is not recognizing the timetable_class since you are in another class (MyJavascriptInterface)? Try changing the line to this:

    Moro.timetable_class[i2].setText(timetable);

    Note, you might also have to declare timetable_class as public:
    Public TextView timetable_class[][] = new TextView[9][5];
     
  3. Ragoune

    Ragoune Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Feb 18, 2011

    Feb 18, 2011
    4
    0
    5
    Hey,

    Sorry for my late reply. Unfortunately it didn't work, I solved it by using a while loop right after the class was created to wait for the variables to be changed, and then I continued the code. A very bad solution of course, but it works for the moment.
     

Share This Page

Loading...