所以我和我的朋友已经完成了我们的代码。但是当我按下ActionBar中的图标时,对话框无法打开。任何帮助将不胜感激。以下是应允许发生警报对话框的代码的特定部分。
protected DialogInterface.OnClickListener mDialogListener =
new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // Take picture
Intent takePhotoIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUri (MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
// display an error
Toast.makeText (UploadActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show ();
} else {
takePhotoIntent.putExtra (MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult (takePhotoIntent, TAKE_PHOTO_REQUEST);
}
break;
case 1: // Take video
Intent videoIntent = new Intent (MediaStore.ACTION_VIDEO_CAPTURE);
mMediaUri = getOutputMediaFileUri (MEDIA_TYPE_VIDEO);
if (mMediaUri == null) {
// display an error
Toast.makeText (UploadActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show ();
} else {
videoIntent.putExtra (MediaStore.EXTRA_OUTPUT, mMediaUri);
videoIntent.putExtra (MediaStore.EXTRA_DURATION_LIMIT, 10);
videoIntent.putExtra (MediaStore.EXTRA_VIDEO_QUALITY, 6); // 0 = lowest res
startActivityForResult (videoIntent, TAKE_VIDEO_REQUEST);
}
break;
case 2: // Choose picture
Intent choosePhotoIntent = new Intent (Intent.ACTION_GET_CONTENT);
choosePhotoIntent.setType ("image/*");
startActivityForResult (choosePhotoIntent, PICK_PHOTO_REQUEST);
break;
case 3: // Choose video
Intent chooseVideoIntent = new Intent (Intent.ACTION_GET_CONTENT);
chooseVideoIntent.setType ("video/*");
Toast.makeText (UploadActivity.this, R.string.video_file_size_warning, Toast.LENGTH_LONG).show ();
startActivityForResult (chooseVideoIntent, PICK_VIDEO_REQUEST);
break;
}
}
private Uri getOutputMediaFileUri(int mediaType) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
if (isExternalStorageAvailable ()) {
// get the URI
// 1. Get the external storage directory
String appName = UploadActivity.this.getString (R.string.app_name);
File mediaStorageDir = new File (
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES),
appName);
// 2. Create our subdirectory
if (!mediaStorageDir.exists ()) {
if (!mediaStorageDir.mkdirs ()) {
Log.e (TAG, "Failed to create directory.");
return null;
}
}
// 3. Create a file name
// 4. Create the file
File mediaFile;
Date now = new Date ();
String timestamp = new SimpleDateFormat ("yyyyMMdd_HHmmss", Locale.US).format (now);
String path = mediaStorageDir.getPath () + File.separator;
if (mediaType == MEDIA_TYPE_IMAGE) {
mediaFile = new File (path + "IMG_" + timestamp + ".jpg");
} else if (mediaType == MEDIA_TYPE_VIDEO) {
mediaFile = new File (path + "VID_" + timestamp + ".mp4");
} else if (mediaType == MEDIA_TYPE_AUDIO) {
mediaFile = new File (path + "Audio_" + timestamp + ".mp3");
} else {
return null;
}
Log.d (TAG, "File: " + Uri.fromFile (mediaFile));
// 5. Return the file's URI
return Uri.fromFile (mediaFile);
} else {
return null;
}
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState ();
if (state.equals (Environment.MEDIA_MOUNTED)) {
return true;
}
else {
return false;
}
}
};
这是代码的onCreate部分:
Map<String, String> networkDetails = getConnectionDetails ();
if (networkDetails.isEmpty ()) {
AlertDialog.Builder builder = new AlertDialog.Builder (UploadActivity.this);
builder.setMessage (R.string.no_internet_connection)
.setTitle (R.string.login_error_title)
.setPositiveButton (android.R.string.ok, new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish ();
}
});
AlertDialog dialog = builder.create ();
dialog.show ();
然后是ActionBar:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId ();
switch(itemId) {
case R.id.action_logout:
ParseUser.logOut ();
navigateToLogin ();
break;
case R.id.action_post:
AlertDialog.Builder builder = new AlertDialog.Builder (UploadActivity.this);
builder.setItems (R.array.camera_choices, mDialogListener);
AlertDialog dialog = builder.create ();
dialog.show ();
break;
}
return super.onOptionsItemSelected(item);
}
}
当我点击动作中的图标时,我真的很困惑为什么它不会打开。我确定我错过了一些我无法理解的东西。
Oncreateoptionsmenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.upload_menu, menu);
return true;
}
menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UploadActivity">
<item android:id="@+id/action_logout"
app:showAsAction="never"
android:title="@string/logout"
android:orderInCategory="300" />
<item android:id="@+id/action_upload"
android:icon="@drawable/ic_upload"
android:title="@string/action_upload"
android:orderInCategory="220"
app:showAsAction="always"/>
</menu>
答案 0 :(得分:1)
您提供两个完全不同的ID。
在menu.xml中,
android:id="@+id/action_upload"
onCreateOptionsMenu你给的是不同的id,
case R.id.action_post:
这应该是一个。
再次onCreateOptionsMenu中的upload_menu你正在膨胀的是你给出的名字是menu.xml。