0

I have problem with an Intent for an array. I want to send an rectangular array to another package. I'm following this tutorial. Unfortunately, I still have a problem with the Intent. So I have the class ActivityToSend. I want to send storePrintIMC into another class (SamplePreferenceActivity) of a different package.

ActivityToSend class:

public class ActivityToSend extends Activity {
    private Activity activity;
    private ArrayList<String[]> data;
    private static LayoutInflater inflater = null;
    public String[] inkLevels = new String[5];
    View message;
    String [][] storePrintIMC= new String [4][4];
    public static final String ARRAYS_COUNT = "com.Status.App.ARRAYS_COUNT";
    public static final String ARRAY_INDEX = "com.Status.App.ARRAY_INDEX"; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String data[][] = storePrintIMC; 
        Bundle bundle = new Bundle();
        int count = data.length; 
        bundle.putInt(ARRAYS_COUNT, count); 
        for (int i = 0; i < count; i++)
        { 
            bundle.putStringArray(ARRAY_INDEX + i, data[i]); 
        } 
        Intent intent = new Intent(this, SamplePreferenceActivity.class); 
        intent.putExtras(bundle);

        startActivity(intent);
    }
}

For the second activity in the different package I use:

public class SamplePreferenceActivity extends PreferenceActivity {    
    private static final int DIALOG_READ_ME = 1;
    public String[] inkLevels = new String[5];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int count = bundle.getInt(ActivityToSend.ARRAYS_COUNT, 0);

            ArrayList<String[]> arrays = new ArrayList<String[]>(count);

            for (int i = 0; i < count; i++)
                arrays.add(bundle.getStringArray(ActivityToSmartWatch.ARRAY_INDEX + i));

            String[][] data = arrays.toArray(new String[][]{});
        }
    }
}

I also made some changes with the Android manifest. For the second package, I put:

<activity android:name=".SamplePreferenceActivity " android:label="@string/preference_activity_title">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.PrinterStatus.AppLab.SamplePreferenceActivity " /> 
  - <intent-filter>
        <action android:name="android.intent.action.MAIN" /> 
    </intent-filter>
</activity>
<activity android:name="com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity" android:parentActivityName="com.PrinterStatus.AppLab.ActivityToSend" /> 
<service android:name="com.sonyericsson.extras.liveware.extension.controlsample.SampleExtensionService" /> 

I get an error in the first package in:

Intent intent = new Intent(this, SamplePreferenceActivity.class);

I don't know what I have to change. Do you have any idea?

honk
  • 7,217
  • 11
  • 62
  • 65
Carlos
  • 59
  • 1
  • 6
  • What's the error? Did you import `SamplePreferenceActivity`? – Ted Hopp Sep 01 '13 at 20:01
  • How can I import it?is it in android manifest or in the class? when I put in class it is not possible to add it – Carlos Sep 01 '13 at 20:05
  • Did you see my answer? Also (not that it makes a difference if you're getting a compiler error), do you really need the `android:parentActivityName` attribute in the manifest? You would only need it if you were starting a `SamplePreferenceActivity` from somewhere other than an `ActivityToSend` activity. – Ted Hopp Sep 01 '13 at 22:17

1 Answers1

1

Since SamplePreferenceActivity is in a different package, you need to import the class into ActivityToSend. It's just like any other import. Judging from what's in your manifest, you need to put this near the top of com.PrinterStatus.AppLab.ActivityToSend.java (with the other import statements):

import com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity;

If you're using Eclipse, you can probably resolve the import automatically using ctrl-shift-O.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
  • yes sir ! one up for your answer !, i misread another package as another app. sorry – Rachit Mishra Sep 01 '13 at 22:26
  • Thank you for your reply. However I still have problem. in class ActivityToSend the error : The constructor Bundle(ActivityToSend, Class) is undefined. – Carlos Sep 02 '13 at 06:12
  • @Carlos - I don't see that code in your original post. Why would you be trying to construct a `Bundle` like that? The arguments look like they ought to be for a new `Intent`, not a new `Bundle`. – Ted Hopp Sep 02 '13 at 07:17
  • @TedHopp- Bundle I used for intent. I refer from this site(http://www.anddev.org/view-layout-resource-problems-f27/how-can-i-pass-multidimensional-string-array-two-activities-t9259.html) Do you have any other solution? – Carlos Sep 02 '13 at 07:43
  • @Carlos - There's no problem using a `Bundle` with an `Intent`. However, they have to be constructed properly. Something like this: `Bundle b = new Bundle(); /* put stuff into b, then: */ Intent intent = new Intent(this, SamplePreferenceActivity.class); intent.putExtras(b); startActivity(intent);`. Note that the `Bundle` constructor takes no arguments. – Ted Hopp Sep 02 '13 at 15:05