1
votes

hi i am passing array of strings from activity A to Activity B. in activity B i am able to get this Array of strings within OnCreate() method .but if i use this array in a Separate class which is outside onCreate() method but in same activity B then this array showing null values. I am declaring the array as a global.

please tell me how to access this array into a seperate class outside the onCreate() method but within same activity...

this is activity A

public class mymap extends Activity { /** Called when the activity is first created. */

public  static  String arr[]={"40.748963847316034","-73.96807193756104" ,"United Nations",
    "40.76866299974387","-73.98268461227417","Lincoln Center",
    "40.765136435316755","-73.97989511489868","Carnegie Hall",
    "40.70686417491799","-74.01572942733765","The Downtown Club",
    "1.352566007"," 103.78921587","Singapore",
    "12.971598","77.594562","Bangalore"};
public  static  String animatel[]={"12.971598","77.594562"};

public static int[] Icons = { R.drawable.drinkingfountain, R.drawable.pin, R.drawable.helicopter, R.drawable.levelcrossing, R.drawable.ter, R.drawable.templehindu

};

public static int[] Icons1 = { R.drawable.icon, R.drawable.pin, R.drawable.f_icon, R.drawable.icon, R.drawable.pin, R.drawable.f_icon

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent intent = new Intent(mymap.this,test.class);
    intent.putExtra("ll",arr );
    intent.putExtra("markers",Icons );
    intent.putExtra("images",Icons1 );
    intent.putExtra("anill", animatel);
    startActivity(intent);
}

}

this is Activity B

public class mapll extends MapActivity { private MapView map=null; private MyLocationOverlay me=null; public static GeoPoint p; public static String[] arr; public static ArrayList Icons; public static ArrayList Icons1; public static Drawable marker1;

public static ArrayList arrtp = new ArrayList(); public static ArrayList Iconstp = new ArrayList(); public static ArrayList Icons1tp = new ArrayList(); public static ArrayList tp = new ArrayList();

String[] animatel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d("prabhu","Inside the ocreate mapll"); arr = getIntent().getStringArrayExtra("ll"); // for(int i=0;i map=(MapView)findViewById(R.id.map);

//map.getController().setCenter(getPoint("40.76793169992044","-73.98180484771729"));
map.getController().setZoom(5);
map.setBuiltInZoomControls(true);


Drawable marker=getResources().getDrawable(R.drawable.pin);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());

me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
map.getOverlays().add(new SitesOverlay(marker));
map.setSatellite(true);

map.getController().animateTo(getPoint(mymap.animatel[0],mymap.animatel[1]));


}

@Override
public void onResume() {
super.onResume();

me.enableCompass();
}

@Override
public void onPause() {
super.onPause();

me.disableCompass();
}

  @Override
protected boolean isRouteDisplayed() {
return(false);
}

  @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}

return(super.onKeyDown(keyCode, event));
}

private GeoPoint getPoint(String arr, String arr2) {
    double lat = Double.parseDouble(arr);
    double lon = Double.parseDouble(arr2);
return(new GeoPoint((int)(lat*1E6),
(int)(lon*1E6)));
}

class SitesOverlay extends ItemizedOverlay<OverlayItem> {

    private List<OverlayItem> items=new ArrayList<OverlayItem>();
    //private Drawable pin=null;
    OverlayItem pi=null;
    public SitesOverlay(Drawable marker) {
    super(marker);
    Log.d("prabhu","Inside the ");
    //this.pin=marker;

// Icons1= getIntent().getExtras().getIntArray("images"); // Icons= getIntent().getExtras().getIntArray("markers"); // arr = getIntent().getExtras().getStringArray("ll"); boundCenterBottom(marker);

    Log.d("prabhu","arr len  "+arr.length);
    for(int i=0;i<mapll.arr.length;i=i+3)
    {

        String add = "";
         Geocoder geoCoder = new Geocoder(

                 getApplicationContext(), Locale.getDefault());
             try {
                //Log.d("prabhu","in try");
                mapll.p= getPoint(mymap.arr[i],mymap.arr[i+1]);
                 List<Address> addresses = geoCoder.getFromLocation(
                        mapll.p.getLatitudeE6()  / 1E6, 
                       mapll.  p.getLongitudeE6() / 1E6, 1);

                 if (addresses.size() > 0) 
                 {

                    Log.d("prabhu","in try");
                     for (int k=0; k<addresses.get(0).getMaxAddressLineIndex(); 
                          k++)

                      add += addresses.get(0).getAddressLine(k) + "\n";
                 Log.d("prabhu","address"+add);
                 }

                 //Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
             }
             catch (IOException e) {  
                   Log.d("prabhu","exception occured");
                 e.printStackTrace();
             }   

// map.getController().animateTo(p); items.add(new OverlayItem(getPoint(mymap.arr[i],mymap.arr[i+1]),"",add));

             add = "";
    }

    for(int m=0;m<mymap.Icons.length;m++){
        mapll. marker1=getResources().getDrawable(mymap.Icons[m]);

        mapll.marker1.setBounds(0, 0,mapll. marker1.getIntrinsicWidth(),
        mapll.marker1.getIntrinsicHeight());
        items.get(m).setMarker(mapll.marker1);
        boundCenterBottom(mapll.marker1);
    Log.d("prabhu","items      "+items.get(m));

    }

    }




    @Override
    protected OverlayItem createItem(int j) {
    return(items.get(j));
    }

    @Override
    protected boolean onTap(int j) {
    //Toast.makeText(NooYawk.this,items.get(j).getSnippet(),Toast.LENGTH_SHORT).show();
        Context mContext = getApplicationContext();
        Dialog dialog = new Dialog(mapll.this);

        dialog.setContentView(R.layout.dialog);
        //dialog.setTitle("Custom Dialog");

        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText(items.get(j).getSnippet());

        ImageView image = (ImageView) dialog.findViewById(R.id.image);

        image.setBackgroundResource(mymap.Icons1[j]);
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);

        return true;


    }

    @Override
    public int size() {
    return(items.size());
    }
    private GeoPoint getPoint(String arr, String arr2) {
        double lat = Double.parseDouble(arr);
        double lon = Double.parseDouble(arr2);
    return(new GeoPoint((int)(lat*1E6),
    (int)(lon*1E6)));
    }
}



}

i am getting this error : FATAL EXCEPTION: main

ERROR/AndroidRuntime(15822): java.lang.NoClassDefFoundError:

ERROR/dalvikvm(15822): Could not find class 'com.prabhu.android.mapll', referenced from method com.prabhu.android.mymap.onCreate ERROR/AndroidRuntime(15822): FATAL EXCEPTION: main ERROR/AndroidRuntime(15822): java.lang.NoClassDefFoundError: com.prabhu.android.mapll ERROR/AndroidRuntime(15822): at com.prabhu.android.mymap.onCreate(mymap.java:49) ERROR/AndroidRuntime(15822): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) ERROR/AndroidRuntime(15822): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) ERROR/AndroidRuntime(15822): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at android.app.ActivityThread.access$2300(ActivityThread.java:126) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at android.os.Handler.dispatchMessage(Handler.java:99) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at android.os.Looper.loop(Looper.java:123) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at android.app.ActivityThread.main(ActivityThread.java:4633) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at java.lang.reflect.Method.invokeNative(Native Method) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at java.lang.reflect.Method.invoke(Method.java:521) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-28 10:28:26.337: ERROR/AndroidRuntime(15822): at dalvik.system.NativeStart.main(Native Method)

2
where are you using the array?Tanmay Mandal
show me your code...i can help you.....Kandha

2 Answers

0
votes

If you make it public static, you can access it from anywhere. You may want to put it in a central Utility class if multiple Activities will need access to it.

0
votes

Make a String array (say strArray variable) in your Activity B and while coming from first Activity A, in onCreate() of Activity B method

strArray = getIntent().getStringArrayExtra("GiveMeArray");

now you can use your array in separate class but within same Activity

Edit:

In your Class mymap while defining the Intent

Intent intent = new Intent(mymap.this,test.class); //  <<< what is test.class
intent.putExtra("ll",arr );

What is test.class, and your Activity B is mapll