【发布时间】: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