2012-08-14 41 views
0
  1. 在我的活動中我有一個AsyncTask,其中我已覆蓋doInBackGround。相機意圖和異步任務的doInBackground衝突

  2. 在相同的活性我有一個照相機意圖打開照相機,並允許用戶在當我呼叫攝像機意圖它是觸發doInBackground方法我推翻採取PIC

問題是。這最終給我一個SingleClientConnManager異常,要求我在再次分配之前釋放客戶端。

這裏是我的活動代碼:

public class UserProfileActivity extends Activity { 

//many instance fiels here 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.user_profile); 

     new LongOperation().execute(""); 

     userImage = (ImageView) findViewById(R.id.profileImage); 
     userName = (TextView) findViewById(R.id.userName_profile); 
     userLocation = (TextView) findViewById(R.id.userLocation_profile); 
     editInfo = (TextView) findViewById(R.id.edit_profile); 
     changeImage = (TextView) findViewById(R.id.changeImage_profile); 
     userScore = (TextView) findViewById(R.id.userScore_profile); 
     friendsLabel = (TextView) findViewById(R.id.userFriends_profile); 


      changeImage.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View arg0) { 
        Intent cameraIntent = new Intent(
          android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
//Point 1 
       } 
      }); 
//Point 2 
    } 
    private class LongOperation extends AsyncTask<String, Void, String> { 

     private InputStream is; 
     private StringBuilder sb; 
     private String result; 
     private ProgressDialog dialog = new ProgressDialog(context); 

     @Override 
     protected String doInBackground(String... params) { 

//Point 3 
      try { 
       HttpResponse response; 

        HttpPost httppost = new HttpPost(
          "http://www.xxxxx.com/yyyy/zzzz"); 
        //httpclient is global to maintain sessions 
        response = SignUpActivity.httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 

       try { 
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(is, "iso-8859-1"), 8); 
        sb = new StringBuilder(); 
        sb.append(reader.readLine() + "\n"); 
        String line = "0"; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        is.close(); 
        result = sb.toString(); 
       } catch (Exception e) { 
        Log.e("error in reading input stream", e.toString()); 
       } 
       try { 
        JSONObject jObj = new JSONObject(result); 
        String status = jObj.getString("status"); 
        score = jObj.getInt("credits"); 
        level = jObj.getInt("level"); 
        image = jObj.getString("image"); 
        fname = jObj.getString("fname"); 
        lname = jObj.getString("lname"); 
        city = jObj.getString("city"); 
        email = jObj.getString("email"); 
        clickedUserId = jObj.getInt("user_id"); 

        JSONArray friendsJsonArray = jObj.getJSONArray("friends"); 
        size = friendsJsonArray.length(); 

        ArrayList<String> friendsNames = new ArrayList<String>(); 
        friendsIds = new int[size]; 
        for (int i = 0; i < size; i++) { 

         friendsNames.add(friendsJsonArray.getJSONObject(i) 
           .getString("name")); 
         friendsIds[i] = friendsJsonArray.getJSONObject(i) 
           .getInt("user_id"); 
        } 
        adapter = new ArrayAdapter<String>(context, 
          R.layout.simple_listview_item, friendsNames); 
       } catch (Exception e) { 

        Log.d("error in creating json object", e.toString()); 
       } 
      } catch (Exception e) { 
//Point 5 
       Log.e("error main try", "Error in http connection" + e.toString()); 
      } 
      return "Executed"; 
     } 
     @Override 
     protected void onPostExecute(String result) { 

      friendsList.setAdapter(adapter); 
      userScore.setText(score + " points" + " level " + level); 
      userName.setText(fname + " " + lname); 
      userLocation.setText(city); 
      changeImage.setText("Change image"); 
      editInfo.setText("Edit"); 
      friendsLabel.setText("Friends"); 
      Bitmap bitmap = null; 
      try { 
       bitmap = BitmapFactory 
         .decodeStream((InputStream) new URL(image).getContent()); 
       userImage.setImageBitmap(bitmap); 
      } catch (MalformedURLException e1) { 

       e1.printStackTrace(); 
       userImage.setImageResource(R.drawable.xxx); 
      } catch (IOException e2) { 

       e2.printStackTrace(); 
       userImage.setImageResource(R.drawable.xxx); 
      } 

      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 

     @Override 
     protected void onPreExecute() { 

      this.dialog.setMessage("Please wait"); 
      this.dialog.show(); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 

     } 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

//Point 4 
     if (resultCode == RESULT_OK) { 
      if (data != null) { 

       photo = (Bitmap) data.getExtras().get("data"); 
       userImage.setImageBitmap(photo); 

      }else{ 

       Intent intent = new Intent(UserProfileActivity.this, 
         UserProfileActivity.class); 
       startActivity(intent); 
      } 
     } else { 

      Intent intent = new Intent(UserProfileActivity.this, 
        UserProfileActivity.class); 
      startActivity(intent); 
     } 
    } 
} 

在這裏的代碼點1,2,3,4,5給出了碼流序列一旦我點擊changeImage TextView的。

請幫我解決這個情況。

謝謝。

回答

0

如果我是正確的問題,是因爲你的設備取向得到改變,當你打開camera.That調用您的活動的onCreate()方法再次 請插入此行到你的活動中menifest,是造成問題

<activity 
      android:name="your activity" android:configChanges="keyboardHidden|orientation" 
      /> 
+0

沒有。我相信它不是因爲方向的改變。我在使用我的應用程序時不改變方向。此外,我已經在所有活動中編寫了這些代碼。在搜索了一整天的解決方案之後,我發現這是因爲相機意圖調用了doInBackground方法。到目前爲止,我從來沒有在同一個活動中使用過異步任務和相機意圖調用。如果你想要的話,你可以通過類似於這個的我的這個問題。 http://stackoverflow.com/questions/11865001/will-onactivityresult-restart-my-activity – 2012-08-14 12:13:15