我试图在两个活动之间传递ArrayList<String[]>
,但似乎无法弄清楚如何做到这一点。
我已尝试putParcelable
并将ArrayList<String[]
投放到Parcelable
,但这给了我错误java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable
。
我也试过putStringArrayList
,但这也不起作用。
感谢您的投入!
主要活动
Intent i = new Intent(context, NewChecklistActivity.class);
i.putExtra("checklists", checklistsArray);
次要活动
checklistsArray = (ArrayList<String[]>) getIntent().getSerializableExtra("checklists");
栈跟踪
01-04 18:49:09.140: E/AndroidRuntime(2246): FATAL EXCEPTION: main
01-04 18:49:09.140: E/AndroidRuntime(2246): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medusa.checkit/com.medusa.checkit.NewChecklistActivity}: java.lang.NullPointerException
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.os.Looper.loop(Looper.java:137)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-04 18:49:09.140: E/AndroidRuntime(2246): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 18:49:09.140: E/AndroidRuntime(2246): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 18:49:09.140: E/AndroidRuntime(2246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 18:49:09.140: E/AndroidRuntime(2246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 18:49:09.140: E/AndroidRuntime(2246): at dalvik.system.NativeStart.main(Native Method)
01-04 18:49:09.140: E/AndroidRuntime(2246): Caused by: java.lang.NullPointerException
01-04 18:49:09.140: E/AndroidRuntime(2246): at com.medusa.checkit.NewChecklistActivity.onCreate(NewChecklistActivity.java:39)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.Activity.performCreate(Activity.java:4470)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-04 18:49:09.140: E/AndroidRuntime(2246): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-04 18:49:09.140: E/AndroidRuntime(2246): ... 11 more
答案 0 :(得分:2)
在提供数据的活动中,使用Intent#putExtra()
:
ArrayList <String []> list = new ArrayList <String[]>();
list.add (new String [] {"a","b"});
Intent i = new Intent (this,YourTargetActivity.class);
i.putExtra("list", list);
startActivity(i);
然后要检索,请使用Intent#getSerializableExtra()
并转换为ArrayList <String[]>
:
ArrayList <String[]> list = (ArrayList<String[]>) getIntent().getSerializableExtra("list");
您需要强制转换,因为编译器无法判断Serializable
实例是否真的是ArrayList<String[]>
。您将获得未经检查的转换警告,这是有道理的,因为这会消除类型安全性。这很好,因为你知道你传递了ArrayList<String[]>
所以检索它不会导致任何ClassCastExceptions。
"a"
改为null
,则当运行时重新创建列表时,将抛出NullPointerException
。
答案 1 :(得分:1)
尝试“i”是意图:
i.putStringArrayListExtra("nameYouWant", nameOfArray);
答案 2 :(得分:0)
您可以使用create singleton Class传递值
public class ShareData{
private volatile static ShareData shareData;
public static ShareData data(){
if(shareData == null){
synchronized (ShareData.class) {
if (shareData == null) {
shareData = new ShareData();
}
}
}
return shareData;
}
public ArrayList<String[]> arrayList;
}
在活动1中设置值
ShareData.data().arrayList = new ArrayList<String[]>();
ShareData.data().arrayList.add(strarray);
获取活动2中的值
public ArrayList<String[]> myArray = ShareData.data().arrayList;