重新启动我的活动

重新启动我的活动

本文介绍了会的onActivityResult()重新启动我的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我设置都喜欢ImageView的意见,使用的AsyncTask他们各自的数据TextViews。

asyncTask.Execute();

我有一个 textView.onCLickListener 这就要求相机和拍摄照片后,在活动中的ImageView设置为这个照片。

但问题是)我的AsyncTask是的onActivityResult(后再次调用;

下面是我的完整活动code:

 公共类UserProfileActivity延伸活动{//许多实例fiels这里
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.user_profile);        新LongOperation()执行();        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(新View.OnClickListener(){                公共无效的onClick(查看为arg0){
                    意图cameraIntent =新意图(
                            android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                    startActivityForResult(cameraIntent,CAMERA_REQUEST);
// 1点
                }
            });
// 2点
    }
    私有类LongOperation扩展的AsyncTask<弦乐,太虚,字符串> {        私人InputStream为;
        私人StringBuilder的SB;
        私人字符串结果;
        私人ProgressDialog对话框=新ProgressDialog(背景);        @覆盖
        保护字符串doInBackground(字符串... PARAMS){// 3点
            尝试{
                HTT presponse响应;                    HttpPost httppost =新HttpPost(
                            http://www.xxxxx.com/yyyy/zzzz);
                    // HttpClient的是全球性的,以保持会话
                    响应= SignUpActivity.httpclient.execute(httppost);
                    HttpEntity实体= response.getEntity();
                    是= entity.getContent();                尝试{
                    读者的BufferedReader =新的BufferedReader(
                            新InputStreamReader的(是ISO-8859-1),8);
                    SB =新的StringBuilder();
                    sb.append(reader.readLine()+\\ n);
                    串行=0;
                    而((行= reader.readLine())!= NULL){
                        sb.append(行+\\ n);
                    }
                    is.close();
                    结果= sb.toString();
                }赶上(例外五){
                    Log.e(错误在读输入流,e.toString());
                }
                尝试{
                    JSONObject的jObj =新的JSONObject(结果);
                    字符串状态= jObj.getString(状态);
                    得分= jObj.getInt(信用);
                    级= jObj.getInt(水平);
                    图像= jObj.getString(图像);
                    FNAME = jObj.getString(其中fname);
                    LNAME = jObj.getString(L-NAME);
                    城市= jObj.getString(城市);
                    电子邮件= jObj.getString(电子邮件);
                    clickedUserId = jObj.getInt(USER_ID);                    JSONArray friendsJsonArray = jObj.getJSONArray(朋友);
                    大小= friendsJsonArray.length();                    ArrayList的<串GT; friendsNames =新的ArrayList<串GT;();
                    friendsIds =新INT [大小]
                    的for(int i = 0; I<大小;我++){                        friendsNames.add(friendsJsonArray.getJSONObject(I)
                                .getString(名字));
                        friendsIds [I] = friendsJsonArray.getJSONObject㈠
                                .getInt(USER_ID);
                    }
                    适配器=新ArrayAdapter<串GT(背景下,
                            R.layout.simple_listview_item,friendsNames);
                }赶上(例外五){                    Log.d(错误创建json对象,e.toString());
                }
            }赶上(例外五){
// 5点
                Log.e(错误主要试试,在HTTP连接错误+ e.toString());
            }
            返回执行的;
        }
        @覆盖
        保护无效onPostExecute(字符串结果){            friendsList.setAdapter(适配器);
            userScore.setText(得分+点+级别+级);
            userName.setText(FNAME ++ LNAME);
            userLocation.setText(市);
            changeImage.setText(更改图片);
            editInfo.setText(编辑);
            friendsLabel.setText(老友记);
            位图位图= NULL;
            尝试{
                位= BitmapFactory
                        由Matchi.com提供回到codeStream((InputStream的)新的URL(图片).getContent());
                userImage.setImageBitmap(位图);
            }赶上(MalformedURLException的E1){                e1.printStackTrace();
                userImage.setImageResource(R.drawable.xxx);
            }赶上(IOException异常E2){                e2.printStackTrace();
                userImage.setImageResource(R.drawable.xxx);
            }            如果(dialog.isShowing()){
                dialog.dismiss();
            }
        }        @覆盖
        在preExecute保护无效(){            this.dialog.setMessage(请稍候);
            this.dialog.show();
        }        @覆盖
        保护无效onProgressUpdate(虚空......值){        }
    }    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){        super.onActivityResult(要求code,结果code,数据);// 4点
        如果(结果code == RESULT_OK){
            如果(数据!= NULL){                。照片=(位图)data.getExtras()获得(数据);
                userImage.setImageBitmap(照片);            }其他{                意向意图=新意图(UserProfileActivity.this,
                        UserProfileActivity.class);
                startActivity(意向);
            }
        }其他{            意向意图=新意图(UserProfileActivity.this,
                    UserProfileActivity.class);
            startActivity(意向);
        }
    }
}


解决方案

摄像头活动是为了在后台运行。

因此​​使用意图呼叫相机

 意图cameraIntent =新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_REQUEST);

将调用doInBackground(字符串... PARAMS)

In my activity I am setting all the Views like ImageView, TextViews with theirs respective data using AsyncTask.

after asyncTask.Execute();

I have a textView.onCLickListener which calls Camera and after the picture is taken, the ImageView in the activity is set to this pic.

But the problem is my asyncTask is called again after the onActivityResult();

Here is my complete Activity code:

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);
        }
    }
}
解决方案

Camera activity is meant to run in background.

Hence calling camera using the intent

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

will call the doInBackground(String... params)

这篇关于会的onActivityResult()重新启动我的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:23