Java-Android将2d int数组从一个活动传递到另一个错误

Java-Android将2d int数组从一个活动传递到另一个错误,第1张

概述活动1:Bundlebundle=newBundle();bundle.putSerializable("CustomLevelData",LevelCreator.LCLevelData);Intenti=newIntent(LevelCreatorPopout.this,GameView.class);i.putExtras(bundle);startActivity(i);活动2:LevelData=(int[][])extras.ge

活动1:

Bundle bundle = new Bundle();bundle.putSerializable("CustomLevelData",  LevelCreator.LCLevelData);Intent i = new Intent(LevelCreatorPopout.this, GameVIEw.class);i.putExtras(bundle);startActivity(i);

活动2:

LevelData=(int[][]) extras.getSerializable("CustomLevelData");

错误:
E / AndroIDRuntime(16220):致命异常:主
E / AndroIDRuntime(16220):java.lang.RuntimeException:无法启动活动ComponentInfo {com.powerpoint45.maze / com.powerpoint45.maze.GameVIEw}:java.lang.classCastException:java.lang.Object []无法投射到int [] []

我已经搜索过,但是在2D INT数组传递中什么也没找到

解决方法:

从性能的角度来看,最好使用Parcelable而不是可序列化,以传递非原始数据.

不知道这是否是最好的主意,但是您可以定义一个包含2d数组并实现Parcelable的类.然后,您可以使用以下方法从活动中传递该类的实例:

Intent intent = this.getIntent();// Assume MyClass is the class which contains the 2d-arrayintent.putExtra("key", myclassObj); //value being the instance/object of MyClass that you want to pass

您可以使用以下方法在另一个活动中检索它:

Intent intent = this.getIntent();Bundle bundle = intent.getExtras();MyClass mc = (MyClass)bundle.getParcelable("key"); 
总结

以上是内存溢出为你收集整理的Java-Android将2d int数组从一个活动传递到另一个错误全部内容,希望文章能够帮你解决Java-Android将2d int数组从一个活动传递到另一个错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1075762.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存