String []通过Intent到另一个Activity的数组(主要活动扩展了片段类)

时间:2017-01-08 20:31:02

标签: android arrays string android-intent

编辑正确的上下文

好吧,以便它在正确的上下文中...我的主要活动有一个片段类,当用户到达片段类时,Main活动上有一个OnClick方法,这是我放置此代码的地方...

这是我尝试过的 活动1

    context = this;
    db = new MyDBEmbrASD(this);
    List<ResourceTable> resources = db.getAllResource();
    String[] imgFPString = new String[resources.size()];
    String[] imgDrawable = new String[resources.size()];
    String[] temprdm = new String[resources.size()];

    //resources = db.getResources(); // you would not typically call this on     the main thread

    System.out.println("TESTESTESTESTESTEST");
    for (int i = 0; i < resources.size(); i++) {
        imgFPString[i] = resources.get(i).getimgFP();
        temprdm[i] = imgFPString[i];
        System.out.println(imgFPString[i]);
        imgDrawable[i] = "drawable-mdpi/" + imgFPString[i];
        System.out.println(imgDrawable[i]);
        temprdm[i] = imgFPString[i];
    }

    List<String> wordList = Arrays.asList(temprdm);
    Collections.shuffle(wordList);
    String[] myShuffledArray = wordList.toArray(new String[wordList.size()]);
    //int essonNumber = 1;
    System.out.println("testestest" + myShuffledArray[1]);
    Intent intent = new Intent(this, DragActivityV2.class);
    intent.putExtra("mStringArray", myShuffledArray);
    startActivity(intent);

活动2

Intent intent = getIntent();
String[] myShuffledArray = intent.getStringArrayExtra("mStringArray");
System.out.println("shfld array " + myShuffledArray[1]);

我得到一个错误  处理:info.androidhive.tabsswipe,PID:25068                   java.lang.RuntimeException:无法启动活动ComponentInfo {info.androidhive.tabsswipe / info.androidhive.tabsswipe.dragview.DragActivityV2}:java.lang.NullPointerException

意图代码基于此处已发布的问题...但我的情况仍然无效...请帮助

编辑这是整个日志错误

E / AndroidRuntime:致命异常:主要                   处理:info.androidhive.tabsswipe,PID:27569                   java.lang.RuntimeException:无法启动活动ComponentInfo {info.androidhive.tabsswipe / info.androidhive.tabsswipe.dragview.DragActivityV2}:java.lang.NullPointerException                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)                       在android.os.Handler.dispatchMessage(Handler.java:110)                       在android.os.Looper.loop(Looper.java:193)                       在android.app.ActivityThread.main(ActivityThread.java:5299)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)                       at dalvik.system.NativeStart.main(Native Method)                    引起:java.lang.NullPointerException                       在info.androidhive.tabsswipe.dragview.DragActivityV2.onCreate(DragActivityV2.java:83)                       在android.app.Activity.performCreate(Activity.java:5264)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)                       在android.os.Handler.dispatchMessage(Handler.java:110)                       在android.os.Looper.loop(Looper.java:193)                       在android.app.ActivityThread.main(ActivityThread.java:5299)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)                       at dalvik.system.NativeStart.main(Native Method) I /处理:发送信号。 PID:27569 SIG:9 申请已终止。

修改

我注意到当我删除了System.out.println(“shfld array”+ myShuffledArray [1]); ...我如何测试/查看我的数组?

EDIT Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.tabsswipe"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.VIBRATE">

</uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="info.androidhive.tabsswipe.MainActivitytest"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="info.androidhive.tabsswipe.DragDropActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="info.androidhive.tabsswipe.DraggableTestingActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="info.androidhive.tabsswipe.MultiImagesTouchActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="info.androidhive.tabsswipe.Main"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="info.androidhive.tabsswipe.dragview.DragActivityV2"
        android:label="@string/app_name" >
    </activity>

</application>



</manifest>

2 个答案:

答案 0 :(得分:1)

尝试使用Bundle将数据发送到ArrayList的下一个活动。您可以按ArrayList<String> data = new ArrayList<String>();添加到ArrayList中,并按data.add("my value");

添加值

答案 1 :(得分:1)

如果您不想使用捆绑机制,那么就这样做(假设这是在活动之间传递的数据):

List<String> wordList = Arrays.asList(temprdm);
 intent.putStringArrayListExtra("mylist", (ArrayList<String>) wordList );

然后在目标活动中

 ArrayList<String> test = getIntent().getStringArrayListExtra("mylist");

至于片段之间的通信以及片段之间的活动通信,它不能直接进行,特别是在片段的活动不是它们的父容器的情况下(通常,对于片段我们的活动)可以通过getActivity()获取片段的父活动的实例。因此,android文档引入了Communicating with Other Fragments,其目的是在片段中构建一个接口,让activity实现它,一旦完成,你就可以在从接口覆盖的方法中进行任何操作。

实际上有一种虚拟方法,不推荐但它可以工作(除了使用监听器接口)。为了使一些属性可以从片段传递到活动,创建一个单独的类来保存一些属性,然后使用这种方法使它们成为静态{(1> , y <考虑到这一点,/ em>和 z 值在不同的组件中是相同的,无论您创建了多少 Fragments / Activity ),例如,在 Fragment 中修改这些值,然后在您创建的任何 Activity / Fragments 中,您只需要通过class Holder { public static x,y,z; },{{1}访问这些值等等......