gpt4 book ai didi

android - 无法在 Actionbar 中实现后退按钮

转载 作者:行者123 更新时间:2023-11-29 21:06:48 25 4
gpt4 key购买 nike

我正在使用 ActionBar 创建一个应用程序。其中操作栏执行共享操作。但我想添加后退按钮以及共享实现。基本上,我希望这两件事都能根据用户的选择工作。

这是我的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}

// Handle item selection
ImageView image = (ImageView) findViewById(R.id.full_image_view); //Unreachable Code
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

File sd = Environment.getExternalStorageDirectory();
String fileName = "desi.png";
File dest = new File(sd, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (item.getItemId()) {
case R.id.item:
Uri uri = Uri.fromFile(dest);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
return true;
default:
return super.onOptionsItemSelected(item);
}

}

我自己实现了这两件事,但以 eclipse 向我提到的无法访问的代码结束。任何帮助将不胜感激。谢谢!

最佳答案

这是正确的代码:

switch (item.getItemId()) {
case R.id.item:
Uri uri = Uri.fromFile(dest);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
return true;


case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}

我尝试在单个 switch 语句中实现这两种情况,这消除了错误,而且应用程序正在根据代码运行。谢谢!

关于android - 无法在 Actionbar 中实现后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24289208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com