• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

.setText in other class causes crash

Ragoune

Lurker
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:
public class Moro extends Activity
	{
	...
	TextView timetable_class[][] = new TextView[9][5];
	...
	public void onCreate(Bundle savedInstanceState)
    		{
		int id_start = 15;
    		for (int i = 0; i < 9; i ++) //Row
    			{
	    		for (int i2 = 0; i2 < 5; i2 ++) //Column
    				{
	    			//Create textviews
	    			timetable_class[i][i2] = new TextView(app);
	    			timetable_class[i][i2].setId(id_start);
	    			
	    			layout_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
	    			...
	    			layout.addView(timetable_class[i][i2], layout_params);
	    			
	    			id_start += 1;
    				}
    			}
		}

	class MyJavaScriptInterface
    		{
        	@SuppressWarnings("unused")
        	public void returnHTML(String html)
        		{
        		int id_start = 15;
        		
	    		int start = html.indexOf("<td>")+1;
	    		for (int i = 0; i < 1; i ++) //Row
	    			{
	    			for (int i2 = 0; i2 < 1; i2 ++) //Column
	    				{
		    			timetable = html.substring(html.indexOf("<td", start), html.indexOf("</td>", start))
		    				.replace("<td>", "")
		    				.replace("<td class=\"vrij\">", "")
		    				.replace("<span class=\"nobr\">", "")
		    				.replace("</span>", "")
		    				.replace("&nbsp;", "")
		    				.trim()
		    				.replace(" W", " ");
		    		
		    			start = html.indexOf("</td>", start)+1;
		    		
		    			timetable_class[i][i2].setText(timetable);
		    			}
	    			}
        		}
    		}

public void setTimetable()
    	{
	...
	web.loadUrl("javascript:window.HTMLOUT.returnHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
	...
    	}

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
 
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];
 
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.
 
Back
Top Bottom