Android getintent()方法崩溃

时间:2017-07-24 18:03:49

标签: android android-intent

我正在使用基本API 11开发Android应用程序。我一直在使用intent的活动之间发送数据,并一直使用getintent()getextras()获取数据。主要是,我已经在API 23中进行了测试,我在api 15中运行相同,它崩溃了。即使是简单的getintent.hasextra也会崩溃。这是兼容性问题吗?我还添加了如果getintent.getextras!= null但这对我也没有帮助。有人可以帮我解决这个问题吗?

谢谢

代码: 活动1:

 Intent detailIntent = new Intent(this,MFGroupDetailsScreen.class);
    detailIntent.putExtra("fromScreen",2);
    if(Constants.USER_ID==selectedGroup.groupOwner)
    {
        Group groupWithAllMembers=groupArray.get(0);
        for(Group group:groupArray)
        {
            if(group.groupID==Constants.GROUP_ID)
            {
                groupWithAllMembers = group;
                break;
            }
        }
        detailIntent.putExtra("members",groupWithAllMembers);
    }
    detailIntent.putExtra("selectedGroup",selectedGroup);
    startActivity(detailIntent);

这里groupwithall成员和selectedGroup是parceable

活动2:

         selectedGroup = (Group) getIntent().getParcelableExtra("selectedGroup");

更新: 错误堆栈跟踪

  

07-25 10:15:44.070:E / ingroupdetails(3391):现在在组详细信息屏幕中   07-25 10:15:46.630:E / AndroidRuntime(3391):致命异常:主要   07-25 10:15:46.630:E / AndroidRuntime(3391):java.lang.RuntimeException:无法启动活动ComponentInfo {com.shimmerresearch.shimmergraphandlogservice / com.shimmerresearch.shimmergraphandlogservice.MFGroupDetailsS​​creen}:java.lang.NullPointerException   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.os.Handler.dispatchMessage(Handler.java:99)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.os.Looper.loop(Looper.java:137)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread.main(ActivityThread.java:5041)   07-25 10:15:46.630:E / AndroidRuntime(3391):at java.lang.reflect.Method.invokeNative(Native Method)   07-25 10:15:46.630:E / AndroidRuntime(3391):at java.lang.reflect.Method.invoke(Method.java:511)   07-25 10:15:46.630:E / AndroidRuntime(3391):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   07-25 10:15:46.630:E / AndroidRuntime(3391):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   07-25 10:15:46.630:E / AndroidRuntime(3391):at dalvik.system.NativeStart.main(Native Method)   07-25 10:15:46.630:E / AndroidRuntime(3391):引起:java.lang.NullPointerException   07-25 10:15:46.630:E / AndroidRuntime(3391):at com.shimmerresearch.shimmergraphandlogservice.MFGroupDetailsS​​creen.onCreate(MFGroupDetailsS​​creen.java:39)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.Activity.performCreate(Activity.java:5104)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)   07-25 10:15:46.630:E / AndroidRuntime(3391):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)   07-25 10:15:46.630:E / AndroidRuntime(3391):......还有11个   07-25 10:15:46.670:E / SurfaceFlinger(1129):ro.sf.lcd_density必须定义为构建属性   07-25 10:15:50.710:E / InputDispatcher(1458):channel' a6f1bc50 com.shimmerresearch.shimmergraphandlogservice / com.shimmerresearch.shimmergraphandlogservice.MFGroupListScreen(server)' 〜频道不可挽回地被打破,将被处置!   07-25 10:15:50.710:E / InputDispatcher(1458):channel' a6bb8580 com.shimmerresearch.shimmergraphandlogservice / com.shimmerresearch.shimmergraphandlogservice.MFSocialScreen(server)' 〜频道不可挽回地被打破,将被处置!   07-25 10:15:50.710:E / InputDispatcher(1458):channel' a6899430 com.shimmerresearch.shimmergraphandlogservice / com.shimmerresearch.shimmergraphandlogservice.DashboardScreen(server)' 〜频道不可挽回地被打破,将被处置!   07-25 10:15:50.740:E / Trace(3432):打开跟踪文件时出错:没有这样的文件或目录(2)   07-25 10:15:50.750:E / jdwp(3432):写入握手字节失败:管道损坏(-1 of 14)   07-25 10:15:50.800:E / SurfaceFlinger(1129):ro.sf.lcd_density必须定义为构建属性   07-25 10:15:50.800:E / InputDispatcher(1458):channel' a732dea8 com.shimmerresearch.shimmergraphandlogservice / com.shimmerresearch.shimmergraphandlogservice.MFSocialScreen(server)' 〜频道不可挽回地被打破,将被处置!   07-25 10:15:50.840:E / SurfaceFlinger(1129):ro.sf.lcd_density必须定义为构建属性   07-25 10:15:50.950:E / emuglGLESv2_enc(1793):检测到超出边界的顶点属性索引。跳过相应的顶点属性。   07-25 10:15:50.950:E / emuglGLESv2_enc(1793):[07-25 10:15:50.990 1458:1473 D /]   07-25 10:15:50.950:E / emuglGLESv2_enc(1793):HostConnection :: get()建立新主机连接0xb9947680,tid 1473   07-25 10:15:51.370:E / emuglGLESv2_enc(1793):检测到超出边界的顶点属性索引。跳过相应的顶点属性。   07-25 10:18:55.930:E / GTalkService(1777):connectionClosed:没有XMPPConnection - 这很奇怪!

2 个答案:

答案 0 :(得分:0)

尝试插入putParcelable并使用getParcelableExtra获取它。

Bundle bundle = new Bundle();
bundle.putParcelable("members",groupWithAllMembers);
bundle.putParcelable("selectedGroup",selectedGroup);

要获取使用 -

Bundle bundle = getIntent().getExtras();
bundle.getParcelable("members");

答案 1 :(得分:0)

在您的群组类中添加以下代码:

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Group createFromParcel(Parcel in) {
            return new Group(in);
        }

        public Group[] newArray(int size) {
            return new Group[size];
        }
    };

以上代码将修复您的错误:android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class

如果您有关于Parcelable实施的任何其他问题,我建议您参考this