I am trying to make an interpreter type thing with my app, where a string from a file on a server is gotten and it is broken down with a split even. Here is the code:
public void onBtnClick(View view)
{
final String message = ((EditText) findViewById(R.id.editText)).getText().toString();
final String mes = message.replace(".meb", "");
final TextView urlTextOut = (TextView) findViewById(R.id.URLtextView);
final ScrollView linearLayout = (ScrollView) findViewById(R.id.setscroll);
new Thread() {
StringBuilder text = new StringBuilder();
@Override
public void run() {
try
{
String str = "";
URL url = new URL("http://awsomisoft.com/" + mes + "/" + mes + ".meb");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
text.append(str);
}
in.close();
} catch (MalformedURLException e1)
{
}
catch (IOException e)
{
}
if(message.contains(".meb")) {
String str[] = text.toString().split("%");
for (final String l : str) {
String code[] = l.split(" ");
if (code[0].toString().equals("$")) {
//Compile
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
final TextView tv = new TextView(getApplicationContext());
tv.setText(l);
linearLayout.addView(tv);
}
});
}
}
}
else {
if(message == null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
urlTextOut.setText("404 Error");
}
});
}
}
}
}.start();
}
I thought running the view element into the uithread would prevent it from crashing, but for some reason it still crashes(all of this is in a thread). What is the problem with the code? Thanks for any help.
EDIT:
Here is the xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="awsomisoft.yemeb.List"
android:id="@+id/List"
android:background="#ffffffff">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/back_web"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="@+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:id="@+id/button2"
android:background="#ffffffff"
android:onClick="onBtnClick"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffffff"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:layout_alignTop="@+id/button2" />
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/setscroll"
android:layout_toEndOf="@+id/URLtextView"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/URLtextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</ScrollView>
</RelativeLayout>
Logcat:
07-28 12:03:38.743 341 535 W ActivityManager: Force removing
ActivityRecord{41038b18 awsomisoft.yemeb/.Main}: app died, no saved state
07-28 12:03:38.873 341 614 W InputMethodManagerService: Got
RemoteException sending setActive(false) notification to pid 2404 uid 10070