java.lang.IllegalArgumentException:ArrayMap中的重复键:null

时间:2016-02-23 21:14:34

标签: android android-intent parcelable

当我试图从意图中获取额外时,我收到此错误。 if (empty($_POST['comment']) === true ) { echo 'ERGHH ! You cannot do this.'; } else //this immediate curly brace ends after the HTML form at the end: { if (isset($_POST['comment'])) { $comment = $_POST['comment']; $comment = mysql_real_escape_string($comment); $sql_register_hangout = mysqli_query($conn, "UPDATE hangout_comments SET (comment, comment_corresponding_hangout_id, comment_corresponding_user_id) SET VALUES ("$comment", "$hangout_id", "$user_data['user_id']") WHERE comment_id=".$_POST['comment']); } /* then there's the HTML form with a input type ="textarea" name="comment" and a submit button input followed by a closing } */

当我更改KEY时,错误变为getIntent().getParcelableArrayListExtra(KEY)。 我无法理解为什么会这样。有许多Parcelable对象工作正常。这也工作得很好(所以,Parcelable对象没有问题),直到我将其他对象置于意图中并使用不同的键值对,并且可能做错了。有人能告诉我为什么会发生这种错误吗?这种错误背后的原因可能是什么?

您也可以查看日志。

java.lang.NullPointerException

当我更改密钥时,我会在日志中得到这个以及空指针异常

02-24 02:55:21.927 28362-28362/com.abc.app E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.abc.app, PID: 28362
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.app/com.abc.app.activity.FinalActivity}: java.lang.IllegalArgumentException: Duplicate key in ArrayMap: null
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2493)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)
         at android.app.ActivityThread.access$800(ActivityThread.java:176)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
         at android.os.Handler.dispatchMessage(Handler.java:111)
         at android.os.Looper.loop(Looper.java:194)
         at android.app.ActivityThread.main(ActivityThread.java:5576)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
      Caused by: java.lang.IllegalArgumentException: Duplicate key in ArrayMap: null
         at android.util.ArrayMap.validate(ArrayMap.java:555)
         at android.os.Parcel.readArrayMapInternal(Parcel.java:2486)
         at android.os.BaseBundle.unparcel(BaseBundle.java:221)
         at android.os.Bundle.getParcelableArrayList(Bundle.java:848)
         at android.content.Intent.getParcelableArrayListExtra(Intent.java:5099)
         at com.abc.app.activity.FinalActivity.onCreate(FinalActivity.java:60)
         at android.app.Activity.performCreate(Activity.java:6005)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555) 
         at android.app.ActivityThread.access$800(ActivityThread.java:176) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 
         at android.os.Handler.dispatchMessage(Handler.java:111) 
         at android.os.Looper.loop(Looper.java:194) 
         at android.app.ActivityThread.main(ActivityThread.java:5576) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 

5 个答案:

答案 0 :(得分:0)

我的Parcelable类出了问题。我重新制作它,现在它工作正常。

答案 1 :(得分:0)

就我而言,我在我的类Model的writeToParcel方法中编写了两次相同的密钥,该方法实现了Parcelable。因此,当它从Bundle保存/检索时,它给我一个错误。

错误的代码:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(feature_id);
    dest.writeString(tab_custom_name);
    dest.writeString(sort_order);
    dest.writeString(tab_logo_selected_image);
    dest.writeString(tab_logo_unselected_image);
    dest.writeString(tab_logo_unselected_image);

所以,我删除了在End中写的两条相似的行中的一行。之后无误。

答案 2 :(得分:0)

在我的情况下,因为服务器语言是php并且它返回json String不是标准的。例如,字段' code'可以是String或int,所以我定义了代码'就这样 Object code。但是当class实现Parcelable时,你必须指定字段类型,如果没有,你将收到错误。

此外,我的解决方案是定义字段' localCode'使用和包裹它。我设置了localCode'当我获得服务器返回数据时的字段。

当然,最好的解决方案是确保服务器返回标准json。

答案 3 :(得分:0)

写入Parcelable并从Parcelable读取的顺序应相同。 或者你把null放在额外内容中。

答案 4 :(得分:0)

在我的情况下,我有两个用于实现Parcelable的类的构造函数,并使用IDE生成了@Override writeToParcel and read parcel方法,它们具有不同数量的参数。因此我通过使用相同的参数(相同的编号,类型和顺序)删除了多余的参数来解决了这些问题,请参见代码中的注释部分。

    public class MediaChannel implements Parcelable {
    String name;
    String url;
    String supportDevice;
    String process;
    String line;
    String icon;

    // movies extra fields
    String imagePath;
    String category;
    String duration;
    String title;
    String description;
    String actors;
    String years;
    String imdb;

    public MediaChannel(String name, String url, String process, String icon, String imagePath, String category, String duration, String title, String description, String actors, String years, String imdb) {
        this.name = name;
        this.url = url;
        this.process = process;
        this.icon = icon;
        this.imagePath = imagePath;
        this.category = category;
        this.duration = duration;
        this.title = title;
        this.description = description;
        this.actors = actors;
        this.years = years;
        this.imdb = imdb;
    }

    public MediaChannel(String name, String url, String supportDevice, String process, String line) {
        this.name = name;
        this.url = url;
        this.supportDevice = supportDevice;
        this.process = process;
        this.line = line;
    }

    protected MediaChannel(Parcel in) {
        name = in.readString();
        url = in.readString();
        supportDevice = in.readString();
        process = in.readString();
        line = in.readString();
    }


    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(url);
        dest.writeString(supportDevice);
        dest.writeString(process);
        dest.writeString(line);
// deleted this part to fix the issues 
/*
       dest.writeString(icon);
       dest.writeString(imagePath);
        dest.writeString(category);
       dest.writeString(duration);
        dest.writeString(title);
       dest.writeString(description);
       dest.writeString(actors);
       dest.writeString(years);
        dest.writeString(imdb);
*/
    }
}