嗨我在活动B中将活动A的字符串数组传递给活动B.我能够在OnCreate()方法中获取这个字符串数组。但是如果我在onCreate之外的Separate类中使用此数组( )但是在相同的活动B中,该数组显示空值。我将数组声明为全局数组。
请告诉我如何在onCreate()方法之外的一个单独的类中访问此数组,但是在相同的活动中...
public class mymap extends Activity { /** 在第一次创建活动时调用。 * /
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);
}
}
公共类mapll扩展MapActivity { private MapView map = null; private MyLocationOverlay me = null; public static GeoPoint p; public static String [] arr; public static ArrayList图标; 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; @覆盖 public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); Log.d(“prabhu”,“在ocreate mapll里面”); arr = getIntent()。getStringArrayExtra(“ll”); // for(int i = 0; i 地图=(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(标记);
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)));
}
}
}
我收到此错误:致命异常:主
错误/ dalvikvm(15822):找不到类com.prabhu.android.mapll',从方法com.prabhu.android.mymap.onCreate中引用 错误/ AndroidRuntime(15822):致命异常:主要 错误/ AndroidRuntime(15822):java.lang.NoClassDefFoundError:com.prabhu.android.mapll ERROR / AndroidRuntime(15822):at com.prabhu.android.mymap.onCreate(mymap.java:49) 错误/ AndroidRuntime(15822):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 错误/ AndroidRuntime(15822):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 错误/ AndroidRuntime(15822):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 04-28 10:28:26.337:ERROR / AndroidRuntime(15822):在android.app.ActivityThread.access $ 2300(ActivityThread.java:126) 04-28 10:28:26.337:ERROR / AndroidRuntime(15822):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2038) 04-28 10:28:26.337:ERROR / AndroidRuntime(15822):在android.os.Handler.dispatchMessage(Handler.java:99) 04-28 10:28:26.337:ERROR / AndroidRuntime(15822):在android.os.Looper.loop(Looper.java:123) 04-28 10:28:26.337:ERROR / AndroidRuntime(15822):在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)
答案 0 :(得分:0)
如果你public static
,你可以从任何地方访问它。如果多个活动需要访问它,您可能希望将其放在一个中心的Utility类中。
答案 1 :(得分:0)
在String
B中制作一个Activity
数组(比如strArray变量),并且来自第一个Activity
A,onCreate()
Activity
B方法
strArray = getIntent().getStringArrayExtra("GiveMeArray");
现在您可以在单独的课程中使用您的数组,但在同一活动中
修改强>
在定义mymap
Intent
中
Intent intent = new Intent(mymap.this,test.class); // <<< what is test.class
intent.putExtra("ll",arr );
什么是test.class
,Activity B
是mapll