问题描述
我正在尝试使用如下switch语句启动活动:
I'm trying to start an activity with a switch statement like that:
public class MainActivity extends AppCompatActivity {
public static String key = "1010";
private Bitmap bitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(R.menu.items, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add:
//working
break;
case R.id.rotate_right:
//working
break;
case R.id.fullscreen: //not working
Intent intent = new Intent(getBaseContext(), fullScreenActivity.class);
intent.putExtra(key, bitmap);
startActivity(intent);
break;
}
}
}
这是下一个活动的代码
public class fullScreenActivity extends AppCompatActivity {
Bitmap bitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent starter = getIntent();
bitmap = starter.getParcelableExtra(MainActivity.key);
}
}
我还向清单中添加了第二个活动:
I also added the second activity to the manifest:
<activity android:name=".fullScreenActivity"
android:screenOrientation="landscape"></activity>
这是R.menu.items:
Here's R.menu.items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add"
android:icon="@drawable/ic_add_black_24dp"
android:title="@string/add_item"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/rotate_right"
android:icon="@drawable/ic_rotate_right_black_24dp"
android:title="@string/rotate_item"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/fullscreen"
android:icon="@drawable/ic_fullscreen_black_24dp"
android:title="@string/fullscreen_item"
app:showAsAction="ifRoom"
/>
</menu>
它什么都不做,那么可能是什么问题?
It does nothing, so what can be the problem?
请注意,仅全屏项目无法正常工作,因此我认为问题出在startActivity代码中.
Note that just the fullscreen item is not working, so I think the problem is in the startActivity code.
推荐答案
我认为问题出在这行
intent.putExtra(key, bitmap);
Android限制意图数据的大小.有2种情况
Android limits size in intent's data. There are 2 cases
- 该应用程序将崩溃并显示
RuntimeException
,您可以轻松地在logcat中看到此异常,如下图所示
- The app will crash with a
RuntimeException
, you can easily see this exception in logcat like below screenshot
- 该应用不会崩溃,但什么也不会显示并返回主屏幕(您的情况).其实有一个关于此问题的警告,要查看该警告,您必须将logcat类型更改为警告,并将logcat过滤器更改为无过滤器,如下面的屏幕截图所示.
- The app won't crash but showing nothing and back to home screen (your case). Actually there is a warning about this issue, to see the warning you must change logcat type to Warn and logcat filter to No Filters like below screenshot.
您可以看到类似812104
这样的警告,对于您来说,情况可能有所不同.
As you can see the warning like, 812104
in my case, it might be different in your case.
android.os.TransactionTooLargeException: data parcel size 812104 bytes
解决方案::如果您需要将位图从活动传递到另一个
Solutions: If you need to pass a bitmap from an activity to another
- 如果位图是URL(文件路径,http/https链接等)或资源ID(R.drawable.splash_screen等),则只需将URL或资源ID传递给另一个活动即可.
- 如果位图来自用户的操作(例如,从相机捕获),则可以将位图保存在本地文件中,然后将文件路径传递给另一个活动.
希望这会有所帮助!
这篇关于startActivity(intent)什么也没做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!