【问题标题】:Adding element to ScrollView crashes app Android SDK向 ScrollView 添加元素会使应用程序 Android SDK 崩溃
【发布时间】:2014-07-28 18:45:57
【问题描述】:

我正在尝试使用我的应用程序制作解释器类型的东西,其中从服务器上的文件中获取一个字符串,并且它被拆分甚至拆分。代码如下:

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();

}

我认为将视图元素运行到 uithread 可以防止它崩溃,但由于某种原因它仍然崩溃(所有这些都在一个线程中)。代码有什么问题?感谢您的帮助。

编辑:

这是活动的xml:

<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

【问题讨论】:

  • +1 @codeMagic 并告诉我们问题所在的行
  • 我只能将我的 Nexus 用于 logcat。我正在寻找一个 logcat 应用程序。
  • 您的 IDE 将显示输出
  • 我的模拟器不工作。我正在我的 Nexus 上测试该应用程序。

标签: android multithreading scrollview


【解决方案1】:

ScrollView 只能有一个孩子

您不能将子海峡添加到ScrollView,因此linearLayout.addView(tv); 将不起作用。 将LinearLayout 作为子级添加到ScrollView,并将TextViews 添加到LinearLayout(或任何GroupView)中。

所以需要先修改XML布局:

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

然后,您需要将它们添加到 id childsContainer

【讨论】:

  • 我该怎么做?它不允许我这样做。
猜你喜欢
  • 2023-03-25
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多