Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序强制使用runnable关闭_Java_Android - Fatal编程技术网

Java 应用程序强制使用runnable关闭

Java 应用程序强制使用runnable关闭,java,android,Java,Android,我必须在android应用程序中点击一个按钮的同时完成一个大任务,所以我使用runnable做后台工作。我的工作很好,没有使用runnable,但大任务会将其冻结一段时间。使用runnable后,我的应用程序在单击按钮时崩溃。 下面是我的按钮onClick函数代码: public void doSearch(View v) { EditText et1 = (EditText) findViewById(R.id.searchText); String query = et1.

我必须在android应用程序中点击一个按钮的同时完成一个大任务,所以我使用runnable做后台工作。我的工作很好,没有使用runnable,但大任务会将其冻结一段时间。使用runnable后,我的应用程序在单击按钮时崩溃。 下面是我的按钮onClick函数代码:

 public void doSearch(View v)
{
    EditText et1 = (EditText) findViewById(R.id.searchText);
    String query = et1.getText().toString();
    Toast.makeText(this, "Searching Lyrics for "+query, Toast.LENGTH_LONG).show();
    final String query1 = query.replaceAll(" ", "+");
    Runnable myRunnable = new Runnable() {
        @Override
        public void run(){
            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet("http://example.com/search.php?q="+query1);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity(); 
                InputStream is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                String line = null;
                int count=0,counter=0, disCount=0;
                String[] name = new String[20];
                String[] link = new String[20];
                String[] dis = new String[20];
                while ((line = reader.readLine()) != null && counter<20){
                    if(count == 1){
                        if(line.contains("href=\"")){
                            line = line.substring(line.indexOf('=')+2);
                            link[counter] = line.substring(0, line.indexOf('"'));
                            line = line.substring(line.indexOf('"')+2);
                            name[counter] = line.substring(0, line.indexOf('<'));
                        }
                        if(disCount==1){
                            if(line.contains("<b>")){
                                line = line.replaceAll("<b>", "");
                            }
                            if(line.contains("</b>")){
                                line = line.replaceAll("</b>", "");
                            }
                            dis[counter] = line+"...";
                            counter++;
                            disCount=0;
                        }
                    }
                    if(line.equals("<div class=\"sen\">")){
                        count = 1;
                    }
                    if(line.equals("<div>")){
                        disCount=1;
                    }
                    if(line.equals("</div>")){
                        count = 0;
                    }
                }
                is.close();
line 82:            searchResult(name, link, dis);
            }catch(IOException e){} catch(IllegalStateException e){}
        }
    };
    Thread myThread = new Thread(myRunnable);
    myThread.start();
}

public void searchResult(String[] name, String[] link, String[] dis)
{
   line 91:     setContentView(R.layout.results);
    nameTemp = name;
    linkTemp = link;
    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < name.length; i++) {
        if(name[i]==null) break;
        if(name[0]==null){
            Toast.makeText(this, "Sorry! No results matched your query. \n Try again! ", Toast.LENGTH_LONG).show();
            }
        RowItem item = new RowItem(name[i], dis[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.result);
    CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(this, "Loading Lyrics for "+nameTemp[position]+"!", Toast.LENGTH_SHORT).show();
    Intent i = new Intent(this, ShowLyricsActivity.class);
    i.putExtra("link", linkTemp[position]);
   startActivity(i); 
}
谁来帮我解决这个错误。
如果您有任何帮助或建议,我们将不胜感激。

我建议您使用此库,而不是像以前那样拨打网络电话

你可以看看这里

您的代码将变得非常非常少,您可以只使用4行代码,而不是声明可以分别编写大量代码

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
    System.out.println(response);
   }
});
它非常有效,可以在2秒内快速获得响应。 您不需要使用缓冲区读取器或其他东西来格式化响应。您可以直接使用onSucces方法的字符串“response”中接收的响应


我希望这能帮到你。:)

在Android中,您只能从主线程修改UI组件。您正在访问从其他线程调用的
searchResult
方法中的组件

你可以这样做:

et1.post(new Runnable() {
    @Override
    public void run() {
        searchResult(...);
    }
});
它将迫使您将一些变量设置为final,以便您可以在匿名对象中使用它们。没关系。见方法


我个人建议您使用类而不是原始Java线程。它包括在后台线程中运行一个方法和在主线程中运行另一个方法

你有两个什么都不做的人!!他们可能隐藏了你的错误!您正在从工作线程调用
searchResult
。您只能从主/UI线程操作UI。因此无需使用asynctask类或runnable或thread废话。@Udit Kapahi您建议的库可以很好地解决此问题,但是,如何向OP解释他不能触摸UI/主线程之外的UI,并显示使用某种处理程序机制或异步任务来操作主线程上的UI。我不明白你想说什么,伙计。@Varun:看看你是否想在网络调用时更新UI,您只需在使用字符串响应的同时在onSuccess()方法中完成所有UI任务。例如:--tv.setText(响应);在内部,一旦接收到JSON或XML内容,onSuccess方法将立即更新textview。如何解析响应并在应用程序中显示数据取决于您。我希望这能解释您的问题。@ashu更新了答案,请参阅
AsyncTask
链接,它有一个清晰的示例。我认为您可以从其他线程访问UI组件,但不能修改它们。例如,可以从
EditText
调用
getText()
,但不能调用
setText(CharSequence)
et1.post(new Runnable() {
    @Override
    public void run() {
        searchResult(...);
    }
});