1

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
4
  • +1 @codeMagic and show us the line where the problem is Commented Jul 28, 2014 at 18:56
  • I am only able to use my Nexus for logcat. I am trying to find a logcat app. Commented Jul 28, 2014 at 19:00
  • Your IDE will show the output Commented Jul 28, 2014 at 19:06
  • My emulator doesn't work. I am testing the app on my Nexus. Commented Jul 28, 2014 at 19:09

1 Answer 1

5

ScrollView can have only one child.

You cannot add child strait to ScrollView, so linearLayout.addView(tv); will not work. Add LinearLayout as a child to ScrollView and add TextViews into LinearLayout (or any GroupView) instead.

So you need to first modify the XML layout:

<ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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">
    <LinearLayout
            android:id="@+id/childsContainer"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/URLtextView"/>
            </LinearLayout>
</ScrollView>

Then instead of adding childs to scrollview (the one with id linearLayout) you need to add them to layout( which you need to inflate first) with id childsContainer

Sign up to request clarification or add additional context in comments.

1 Comment

How can I do that? It won't allow me to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.