StartActivityForResult 正在一项活动中工作但不在其他活动中工作(访问相同的功能)。 我有3个活动,分别是A,B& C. B& B C继承自A.我在父活动中编写了一个代码,并在两个子活动中访问它,同样的功能在B中工作但不在C活动中工作。我已经在调试模式中逐行检查了onActivityResult,从未在C活动中调用过。
这是代码
以下是在父活动A中编写的PhotoDialog函数
public void PhotoDialog() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.snapshot_dialog);
dialog.setCancelable(true);
final TextView item1 = (TextView) dialog.findViewById(R.id.dialogItem1);
final TextView item2 = (TextView) dialog.findViewById(R.id.dialogItem2);
item1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent pictureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureIntent, CAMERA_REQUEST);
dialog.dismiss();
}
});
item2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strPrompt = "Choose a Snap Shot!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(
Intent.createChooser(pickPhoto, strPrompt),
GALLERY_REQUEST);
dialog.dismiss();
}
});
dialog.show();
}
以下是在父活动中编写的onClick侦听器。
public void btnCameraIconOnClick(View view) {
PhotoDialog();
}
这是活动B和C的xml
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="btnCameraIconOnClick"> <---- Here is onClick
<ImageView
android:id="@+id/ivProfilePic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:background="@drawable/profile_pic_frame" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="-5dp"
android:layout_marginRight="-10dp"
android:src="@drawable/camera_icon" />
</FrameLayout>
请指导我做错了什么。为什么它在B中工作而不在C中工作。两种活动的代码没有区别。
更新 C活动在tabview中。
修改
<permission
android:name="com.nthreads.out2b.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.nthreads.out2b.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activity.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.B"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".activity.C"
android:screenOrientation="portrait" >
</activity>
</application>
此代码写在B和C两个地方,我正在调用 PhotoDialog
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_CANCELED) {
// Avatar camera mode was canceled.
} else if (resultCode == Activity.RESULT_OK) {
//
Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
if (cameraPic != null) {
try {
setProfilePic(cameraPic);
} catch (Exception e) {
Log.e("TAG", "saveAvatar() with camera image failed.",
e);
} catch (OutOfMemoryError e) {
Log.e("TAG", e.toString());
Toast.makeText(getApplicationContext(),
"OutOfMemory Exception", Toast.LENGTH_LONG)
.show();
}
}
}
break;
case GALLERY_REQUEST:
if (resultCode == Activity.RESULT_CANCELED) {
// Avatar gallery request mode was canceled.
} else if (resultCode == Activity.RESULT_OK) {
// Determine which picture was chosen
Uri photoUri = data.getData();
if (photoUri != null) {
try {
setProfilePic(loadImageFromUri(photoUri));
} catch (Exception e) {
Log.e("TAG",
"saveAvatar() with gallery picker failed.", e);
} catch (OutOfMemoryError e) {
Log.e("TAG", e.toString());
Toast.makeText(getApplicationContext(),
"OutOfMemory Exception", Toast.LENGTH_LONG)
.show();
}
}
}
break;
}
}
private void setProfilePic(Bitmap bitmap) {
Bitmap scaledPic = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
profilePic.setImageBitmap(scaledPic);
}