首先应该在Activity中用frament引用一下onActivityResult(),例如fragmentdemo. onActivityResult(),这里不用写父类的super. onActivityResult(),
<span style="white-space:pre"> </span>public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub <span style="color:#ff6666;">cuPhotoVoiceItem</span>.onActivityResult(requestCode, resultCode, data); Log.e(getClass().getName()+"onActivityResult", getClass().getName()+"onActiv<span style="white-space:pre"> </span>ityResult"); }
这里要记住,fragmentdemo是继承了fragment的一个对象,在它相应的fragment类中要写onActivityResult的真正要跳转的具体类容,这里要写父类的super. onActivityResult(),
public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub <span style="color:#cccccc;background-color: rgb(255, 0, 0);">super.onActivityResult(requestCode, resultCode, data);</span> Log.e(getClass().getName()+"onActivityResult", getClass().getName()+"onActivityResult"); if (resultCode == Activity.RESULT_OK && requestCode == 1) { Bundle b = new Bundle(); b.putString("imagePath", photo1RealPath); Intent intent = new Intent(context, ChoosephotoActivity.class); flag = 1; intent.putExtras(b); ((FragmentActivity) context).startActivityForResult(intent, 5); } else if (resultCode == Activity.RESULT_OK && requestCode == 2) { Bundle bundle = new Bundle(); Uri uri = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Log.e("picture", uri.toString()); Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); photo1RealPath = cursor.getString(columnIndex); cursor.close(); bundle.putString("imagePath", photo1RealPath); Intent intent = new Intent(context, ChoosephotoActivity.class); flag = 2; intent.putExtras(bundle); ((FragmentActivity) context).startActivityForResult(intent, 5); } else if (resultCode == Activity.RESULT_OK && requestCode == 3) { Bundle b1 = new Bundle(); b1.putString("imagePath", photo2RealPath); Intent intent1 = new Intent(context, ChoosephotoActivity.class); flag = 1; intent1.putExtras(b1); ((FragmentActivity) context).startActivityForResult(intent1, 6); } else if (resultCode == Activity.RESULT_OK && requestCode == 4) { Bundle bundle = new Bundle(); Uri uri = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Log.e("picture", uri.toString()); Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); photo2RealPath = cursor.getString(columnIndex); cursor.close(); bundle.putString("imagePath", photo2RealPath); Intent intent = new Intent(context, ChoosephotoActivity.class); flag = 2; intent.putExtras(bundle); ((FragmentActivity) context).startActivityForResult(intent, 6); } else if (resultCode == Activity.RESULT_OK && requestCode == 5) { photo1Path = data.getStringExtra("imagePath"); photo1RealPath = Config.IMAGE_PATH + photo1Path; } else if (resultCode == Activity.RESULT_OK && requestCode == 6) { photo2Path = data.getStringExtra("imagePath"); photo2RealPath = Config.IMAGE_PATH + photo2Path; } else if (resultCode == Activity.RESULT_OK && requestCode == 7) { voicePath = data.getStringExtra("voiceName"); voiceTimeStr = data.getIntExtra("voiceTime", 0) + "'"; } if (!photo1Path.equals("")) { badgeImage1.show(); badgeImage1.setOnClickListener(badgeImage1Listener); ImageLoader.getInstance() .displayImage("file://" + Config.IMAGE_PATH + photo1Path, photo1, options); context.getResources().getDrawable( R.drawable.defect_compose_pic_add_highlighted); Log.e("获取到的photo1Path", "photo1Path"+photo1Path); photo2.setVisibility(View.VISIBLE); photo2.setOnClickListener(this); } if (!photo2Path.equals("")) { badgeImage1.show(); badgeImage1.setOnClickListener(badgeImage2Listener); ImageLoader.getInstance() .displayImage("file://" + Config.IMAGE_PATH + photo2Path, photo2, options); Log.e("获取到的photo2Path", "photo2Path"+photo2Path); } if (!voicePath.equals("")) { voice.setBackground(context.getResources().getDrawable( R.drawable.record_voice)); //initBadgeView(badgeVoice); badgeVoice.show(); badgeVoice.setOnClickListener(badgeVoiceListener); voiceTime.setVisibility(View.VISIBLE); voiceTime.setText(voiceTimeStr + "'"); } }
即这个继承了fragment的类里面的父类的onActivityResult()的实现是在执行父类的也就是Activity中的父类的onActivityResult()后,跳转到fragment中的这个onActivityResult().说白了,宿主(Activity)中的onActivityResult()只是一个跳转的中继点,在fragment中跳转的是真正的跳转点,并且在fragment中实现跳转或获取资源时前面要加context或向上转型为FragmentActivity,否则会出现错误:该fragment未绑定Activity.
刚开始我的错误就是在Activity中没有写onActivityResult(),然后一直报错:没有绑定activity。只是在fragment中写了onActivityResult,然后无论怎么该都没有进行onActivityResult里面的程序,也就是断点进不来,按一般网上说的就是说在Activity里面用一个fragment的对象,它首先不会实现fragment类里面的onActivityResult,也就是说会出现空指针错误,然后就是写了在Activity里面写了onActivityResult,但是没有用fragment对象来关联它,也就是说没有写fragmentdemo. onActivityResult();我只是照搬写了super. onActivityResult();其次,写好了这个,我们要在fragment类中用引用上下文,也就是与在之前的关联,建立起真正的完整联系,这样就能实现startActivityforResult与onActivityResult跳转了。
可能每个人遇到的问题不一样,我遇到的就是这样的,如果大家觉得我的方法有用,可以点个赞,如果还有问题,可以交流一下~!