gpt4 book ai didi

android - 将 java.util.map 传递给另一个 Activity

转载 作者:行者123 更新时间:2023-11-30 01:31:57 32 4
gpt4 key购买 nike

我正在使用 Jsoup 解析网站.
我有一个 Map<String, String> cookiesMainActivity .
我需要传递值 cookiesSecondActivity .
所以我创建了一个类 implements Pracelable .

主 Activity 类:

DataPasser dataPass = new DataPasser(id, pw, cookies);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", dataPass);
MainActivity.this.startActivity(intent);


第二个 Activity 类:

dataPasser dataPass = getIntent().getParcelableExtra("data");
String id = dataPass.getId();
String pw = dataPass.getPw();
Map<String, String> cookies = dataPass.getCookies();


类实现Parcelable :

public class DataPasser implements Parcelable {

private String id;
private String pw;
Map<String, String> cookies;

public String getId() {
return id;
}

public String getPw() {
return pw;
}

public Map<String, String> getCookies() {
return cookies;
}

public DataPasser(Parcel in) {
this.id = in.readString();
this.pw = in.readString();
in.readMap(cookies, Map.class.getClassLoader()); /** NullPointerException here. **/
}

public DataPasser(String id, String pw, Map<String, String> cookies) {
this.id = id;
this.pw = pw;
this.cookies = cookies;
}


@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(id);
out.writeString(pw);
out.writeMap(cookies);
}

public int describeContents() {
return 0;
}

public static final Parcelable.Creator<DataPasser> CREATOR = new Parcelable.Creator<DataPasser>() {
public DataPasser createFromParcel(Parcel in) {
return new DataPasser(in);
}
public DataPasser[] newArray (int size) {
return new DataPasser[size];
}
};
}


错误日志:

FATAL EXCEPTION: main Process: com.hsk.hallymloginprac, PID: 8071 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hsk.hallymloginprac/com.hsk.hallymloginprac.MenuActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) at android.app.ActivityThread.access$900(ActivityThread.java:170) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5635) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.os.Parcel.readMapInternal(Parcel.java:2305) at android.os.Parcel.readMap(Parcel.java:1575) at com.hsk.hallymloginprac.DataPasser.(DataPasser.java:37) at com.hsk.hallymloginprac.DataPasser$1.createFromParcel(DataPasser.java:83) at com.hsk.hallymloginprac.DataPasser$1.createFromParcel(DataPasser.java:81) at android.os.Parcel.readParcelable(Parcel.java:2111) at android.os.Parcel.readValue(Parcel.java:2020) at android.os.Parcel.readArrayMapInternal(Parcel.java:2321) at android.os.Bundle.unparcel(Bundle.java:249) at android.os.Bundle.getParcelable(Bundle.java:1206) at android.content.Intent.getParcelableExtra(Intent.java:5292) at com.hsk.hallymloginprac.MenuActivity.onCreate(MenuActivity.java:44) at android.app.Activity.performCreate(Activity.java:5580) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)  at android.app.ActivityThread.access$900(ActivityThread.java:170)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5635)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)  at dalvik.system.NativeStart.main(Native Method) 

但是,我得到了一个 NullPointerExceptionDataPasser类(class)。
我怎样才能通过 Map<String, String>到另一个 Activity ?

最佳答案

我无法确定您的实际问题,但我可以建议使用 writeBundle而不是 writeMap .同样根据文档,强烈建议使用 writeBundle(Bundle) 而不是此方法,因为 Bundle 类提供了类型安全的 API,可让您避免神秘的类型错误编码。

您可以使用 readBundle() 轻松读取数据这是用 writeBundle 写的.它会从当前 dataPosition() 的包裹中读取并返回一个新的 Bundle 对象。如果再次提出问题,请发表评论。

更新

以 Person 为例,您的 writeToParcel 看起来像这样,

 @Override
public void writeToParcel(Parcel dest, int flags) {
// create a bundle for the key value pairs
Bundle bundle = new Bundle();

// insert the key value pairs to the bundle
bundle.putString(KEY_NAME, name);
bundle.putInt(KEY_AGE, age);

// write the key value pairs to the parcel
dest.writeBundle(bundle);
}

createFromParcel 方法如下所示,

@Override
public Person createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();

// instantiate a person using values from the bundle
return new Person(bundle.getString(KEY_NAME),
bundle.getInt(KEY_AGE));
}

关于android - 将 java.util.map 传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35618027/

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