如何处理 - 应用程序已停止在Android中工作

问题描述:

因此,我正在尝试创建一个Android应用程序,它基本上根据UI内的搜索查询读出Twitter提要。我需要从解析的JSON中显示的提要是用户名,句柄,个人资料图片和推文。如何处理 - 应用程序已停止在Android中工作

现在我已经创建了所有东西,并且我的代码编译完成了,但只要我运行它,应用程序就会打开,然后我在搜索Feed中写入内容并敲入回车 - “不幸的是,AppName已停止工作”和我的源代码供参考。

*通过从DoInBackground中删除设置文本解决了该问题,然后给予Android足够的权限以访问互联网。现在的问题是,当我尝试并显示个人资料图片时,显示的是网址,而不是图片。

的源代码:

package com.example.twittersearchactivity; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.StatusLine; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class TwitterSearchActivity extends Activity { 

    private TextView tweetDisplay; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_twitter_search); 
     tweetDisplay = (TextView)findViewById(R.id.tweet_txt); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.twitter_search, menu); 
     return true; 
    } 
    public void searchTwitter(View view){ 
    EditText searchTxt = (EditText)findViewById(R.id.search_edit); 
    String searchTerm = searchTxt.getText().toString(); 
    if(searchTerm.length()>0){ 
     try{ 
      String encodedSearch = URLEncoder.encode(searchTerm, "UTF-8"); 
      String searchURL = "http://search.twitter.com/search.json?q="+encodedSearch; 
      new GetTweets().execute(searchURL); 
      Log.i("1", "entered the searchterm"); 
     } 
     catch(Exception e){ 
      tweetDisplay.setText("Whoops - something went wrong!"); 
      e.printStackTrace(); 
     } 
    } 
    else 
     tweetDisplay.setText("Enter a search query!"); 
    } 
    private class GetTweets extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... twitterURL) { 
      StringBuilder tweetFeedBuilder = new StringBuilder(); 
      for (String searchURL : twitterURL) { 
       HttpClient tweetClient = new DefaultHttpClient(); 
       try { 
        HttpGet tweetGet = new HttpGet(searchURL); 
        HttpResponse tweetResponse = tweetClient.execute(tweetGet); 
        StatusLine searchStatus = tweetResponse.getStatusLine(); 

        if (searchStatus.getStatusCode() == 200) { 
         HttpEntity tweetEntity = tweetResponse.getEntity(); 
         Log.i("2", "entered gettweets"); 
         InputStream tweetContent = tweetEntity.getContent(); 
         InputStreamReader tweetInput = new InputStreamReader(tweetContent); 
         BufferedReader tweetReader = new BufferedReader(tweetInput); 
         String lineIn; 
         while ((lineIn = tweetReader.readLine()) != null) { 
          tweetFeedBuilder.append(lineIn); 
          Log.i("3", "entered while in dobackground"); 
         } 
        } 
        else {Log.i("error", "error");} 
         //tweetDisplay.setText("Whoops - something went wrong!"); 
       } 
       catch(Exception e) { 
        Log.e("DEBUGTAG", "Remote Image Exception", e); 
        //tweetDisplay.setText("Whoops - something went wrong!"); 
        e.printStackTrace(); 
       }} 

      return tweetFeedBuilder.toString(); 
     } 
     protected void onPostExecute(String result) { 
      StringBuilder y; 
      StringBuilder tweetResultBuilder = new StringBuilder(); 
      try { 
       Log.i("tag", "entered try block"); 
       JSONObject resultObject = new JSONObject(result); 
       JSONArray tweetArray = resultObject.getJSONArray("results"); 
       for (int t=0; t<tweetArray.length(); t++) { 
        Log.i("tag", "entered the json stream"); 
        JSONObject tweetObject = tweetArray.getJSONObject(t); 
        tweetResultBuilder.append(tweetObject.getString("from_user")+": "); 
        tweetResultBuilder.append(tweetObject.getString("from_user_name")+": "); 
        tweetResultBuilder.append(tweetObject.get("text")+"\n\n"); 
        String imageURL = (String) tweetObject.get(("profile_image_url")+": "); 
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent()); 
        @SuppressWarnings("deprecation") 
        Drawable d =new BitmapDrawable(bitmap); 
        d.setAlpha(255); 
        TextView.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0); 
        } 
      } 
      catch (Exception e) { 
       tweetDisplay.setText("Whoops - something went wrong!"); 
       e.printStackTrace();} 

      if(tweetResultBuilder.length()>0) 
       tweetDisplay.setText(tweetResultBuilder.toString()); 
      else 
       tweetDisplay.setText("Sorry - no tweets found for your search!"); 
     } 
    }} 

不能调用像setText视图功能在另一个线程就像一个AsyncTaskdoInBackground功能。您需要在onPostExecute中完成。

+0

是否这样做,在AsyncTask doInBackground中同时评论了setText,但现在在搜索查询时,即使Web浏览器中的类似查询显示结果,也只是显示“抱歉,找不到推文”。 – user1795516 2013-02-26 06:57:56

+0

听起来像是一个单独的问题。我会在调试器下运行它,看看哪里出了问题。 – 2013-02-26 06:59:01

+0

logcat for debugger:https://www.dropbox.com/s/du3le8dshr9n090/log.txt – user1795516 2013-02-26 07:10:37