在Android Studio线程中检索editText时应用程序崩溃



这类似于我之前与textView的另一个问题,涉及应用程序的崩溃(已解决),但现在我遇到了editText的问题,因为每当我尝试检索editText.getvalue()时,即使它在线程之外,它也会崩溃应用程序。

代码如下:

package awsomisoft.yemeb;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import awsomisoft.yemeb.R;
public class List extends Activity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    final TextView urlTextOut = (TextView) findViewById(R.id.URLtextView);
    final EditText sear = (EditText) findViewById(R.id.editText);
    new Thread() {
        StringBuilder text = new StringBuilder();
        @Override
        public void run() {
            try
            {
                String str = "";
                URL url = new URL("http://mydomainname.com/"+sear.getText().toString()+"/test.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)
            {
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    urlTextOut.setText(text);
                }
            });
        }
    }.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

编辑:这里是activity_list.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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="awsomisoft.yemeb.List"
android:id="@+id/List">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/URLtextView"
    android:textSize="18dp" />
</RelativeLayout>

我试图使用处理程序之前,因为有一个解决方案,我发现在这里使用editText线程,但它没有解决问题。我认为这个问题有解决办法,但我似乎找不到。我只需要一个指南,看看我需要做什么与editText,以确保应用程序不会崩溃。谢谢你的帮助。如果有简单的解决办法,我很抱歉。

通常情况下,您可以通过声明变量为final来允许匿名类访问外部类变量。然而,在您的情况下,由于这是处理编辑文本,您希望该变量能够更改,因此final是不可接受的。

在这种情况下,最好的办法是使线程成为一个普通类,而不是一个匿名类,然后在构造函数中传递变量(它需要是一个普通类,因为你不能在匿名类中显式声明构造函数)。

public class MyThread implements Runnable {
    StringBuilder text = new StringBuilder();
    String editTextContents;
    // Constructor
    public MyThread(String editTextContents) {      // <-- Pass in the string from EditText here
        this.editTextContents = editTextContents;
    }
    @Override
    public void run() {
        try
        {
            String str = "";
            URL url = new URL("http://mydomainname.com/"+ editTextContents +"/test.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)
        {
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                urlTextOut.setText(text);
            }
        }
    }
}

你可以在activity中调用这个线程:

Runnable myRunnable = new MyThread(editTextContents);
new Thread(myRunnable).start;

如果你试图访问的EditText不在你正在使用

设置的布局中
setContentView(R.layout.activity_list);

那么它将把sear设置为一个它找不到的视图或者(null):

final EditText sear = (EditText) findViewById(R.id.editText); // null
sear.getText().toString() // will now crash your app because
                          // you're trying to access a null object

换句话说,R.id.editText需要在布局activity_list。xml中或者你需要使用其他xml文件当你setContentView(*.xml)

相关内容

最新更新