我正在做地理编码应用。这个应用程序包含一个主Activity,一个按钮用于启动Google Maps API(片段),另外两个textviews必须显示纬度和经度坐标。
当我启动地图时,我必须能够长按任意点,并且此点的坐标必须返回到主活动并显示在Textviews上。
这是GoogleMapsFragments的代码,我触及了这一点:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//...
mMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
mLatLng = arg0;
ReturnMapData(mLatLng);
}
});
}
public void ReturnMapData(LatLng arg) {
mCodingActivity.GetLocationFromMap(mLatLng);
}
方法GetLocationFromMap属于主Activity。也许这是失败,我不知道如果这是最好的方法。
无论如何,主Activity中的这个方法有这样的看法:
public void GetLocationFromMap(LatLng arg0) {
String latitud = String.valueOf(arg0.latitude);
String longitud = String.valueOf(arg0.longitude);
txtLatitude.setText(latitud);
txtLongitude.setText(longitud);
}
但是我在这一行txtLatitude.setText(latitud);
这是LogCat上显示的内容:java.lang.NullPointerException
更新 - 添加了LogCat
02-05 13:24:06.102: E/AndroidRuntime(8907): FATAL EXCEPTION: main
02-05 13:24:06.102: E/AndroidRuntime(8907): java.lang.NullPointerException
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.final.reversegeocoding.GeocodingActivity.GetLocationFromMap(GeocodingActivity.java:231)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.final.reversegeocoding.GoogleMapsFragment.ReturnMapData(GoogleMapsFragment.java:84)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.final.reversegeocoding.GoogleMapsFragment$1.onMapLongClick(GoogleMapsFragment.java:70)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.google.android.gms.maps.GoogleMap$7.onMapLongClick(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.google.android.gms.maps.internal.j$a.onTransact(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at android.os.Binder.transact(Binder.java:347)
02-05 13:24:06.102: E/AndroidRuntime(8907): at ecp.a(SourceFile:93)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.ae.j.a(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.ao.an.c(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.ao.bc.onLongPress(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.br.g.onLongPress(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.br.h.c(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at maps.br.i.handleMessage(Unknown Source)
02-05 13:24:06.102: E/AndroidRuntime(8907): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 13:24:06.102: E/AndroidRuntime(8907): at android.os.Looper.loop(Looper.java:137)
02-05 13:24:06.102: E/AndroidRuntime(8907): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-05 13:24:06.102: E/AndroidRuntime(8907): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 13:24:06.102: E/AndroidRuntime(8907): at java.lang.reflect.Method.invoke(Method.java:525)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-05 13:24:06.102: E/AndroidRuntime(8907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-05 13:24:06.102: E/AndroidRuntime(8907): at dalvik.system.NativeStart.main(Native Method)
更新2 - 我如何定义textlatitude:
private EditText txtLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
txtLatitude = (EditText) findViewById(R.id.latitude_edit);
更新3 - 我如何调用MAP片段
View mapFrame = findViewById(R.id.map_fragment);
mDualPane = mapFrame != null && mapFrame.getVisibility() == View.VISIBLE;
if (mDualPane){
Fragment mapFrag = getSupportFragmentManager().findFragmentById(R.id.map_fragment);
GoogleMapsFragment mapsFragment = new GoogleMapsFragment();
if (mapFrag == null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.map_fragment, mapsFragment).commit();
}else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.map_fragment, mapsFragment).commit();
}
} else {
Intent intent = new Intent();
intent.setClass(GeocodingActivity.this, GoogleMapsActivity.class);
startActivity(intent);
}
如果是智能手机(sinlgePane),那就是我现在正在测试的那个,我称之为具有下一代码的GoogleMapsActivity。此活动只是调用片段的转换活动。
public class GoogleMapsActivity extends FragmentActivity {
GoogleMapsFragment mapsFragment = new GoogleMapsFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, mapsFragment).commit();
}
更新4 - 更新了ReturnMapData方法
public void ReturnMapData(LatLng arg) {
Intent intent = getActivity().getIntent();
Bundle args = new Bundle();
args.putParcelable("maps_location", arg);
intent.putExtra("bundle", args);
getActivity().setResult(getActivity().RESULT_OK, intent);
View mapFrame = getActivity().findViewById(R.id.map_fragment);
mDualPane = mapFrame != null && mapFrame.getVisibility() == View.VISIBLE;
if (!mDualPane){
getActivity().finish();
}
}
更新5 -
使用onActivityResult修改代码以从MapFragment获取数据,但在获取此数据时仍然存在一些问题。这就是我的方式:
从活动中调用MapsFrament:
Intent intent = new Intent();
intent.setClass(GeocodingActivity.this, GoogleMapsActivity.class);
startActivityForResult(intent, REQUEST_CODE_MAP_LOCATION);
将结果发回主活动:
public void ReturnMapData(LatLng arg) {
Intent intent = getActivity().getIntent();
Bundle bundle = new Bundle();
bundle.putParcelable("maps_location", arg);
intent.putExtra("bundle", bundle);
getActivity().setResult(getActivity().RESULT_OK, intent);
}
获取主要活动的结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_MAP_LOCATION) {
if(resultCode == RESULT_OK){
Bundle bundle = data.getBundleExtra("bundle");
LatLng coordinates = bundle.getParcelable("maps_location");
mLocation.setLatitude(coordinates.latitude);
mLocation.setLongitude(coordinates.longitude);
showLocation(mLocation);
}
}
但是,这就是我在LogCat上得到的:
02-06 09:26:43.210: E/AndroidRuntime(20541): FATAL EXCEPTION: main
02-06 09:26:43.210: E/AndroidRuntime(20541): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { cmp=com.uax_final.reversegeocoding/.GoogleMapsActivity (has extras) }} to activity {com.uax_final.reversegeocoding/com.uax_final.reversegeocoding.GeocodingActivity}: java.lang.NullPointerException
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread.access$1200(ActivityThread.java:159)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.os.Looper.loop(Looper.java:137)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-06 09:26:43.210: E/AndroidRuntime(20541): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 09:26:43.210: E/AndroidRuntime(20541): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 09:26:43.210: E/AndroidRuntime(20541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-06 09:26:43.210: E/AndroidRuntime(20541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-06 09:26:43.210: E/AndroidRuntime(20541): at dalvik.system.NativeStart.main(Native Method)
02-06 09:26:43.210: E/AndroidRuntime(20541): Caused by: java.lang.NullPointerException
02-06 09:26:43.210: E/AndroidRuntime(20541): at com.uax_final.reversegeocoding.GeocodingActivity.onActivityResult(GeocodingActivity.java:232)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.Activity.dispatchActivityResult(Activity.java:5563)
02-06 09:26:43.210: E/AndroidRuntime(20541): at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
答案 0 :(得分:1)
txtLatitude为null(在堆栈跟踪中很容易看到)。
你的
txtLatitude = (EditText) findViewById(R.id.latitude_edit);
没有用,或者你在GetLocationFromMap之前的某个时候用tt覆盖了txtLatitude。
请尝试检查:
txtLatitude = (EditText) findViewById(R.id.latitude_edit);
System.err.println("txtLatitude exists: "+(txtLatitude!=null));
你应该在Logcat中看到输出。
如果为null,请尝试重新编译项目(clean build)。有时IDE会卡住并且无法正确编译新类。如果这不起作用,也许findViewById(R.id.latitude_edit)不会返回EditText,然后在转换时丢失。您可以使用以下方法检查:
System.err.println("latitude_edit is EditText: "+(findViewById(R.id.latitude_edit) instanceof EditText);
如果它实际上不是null,你将必须在整个程序中检查txtLatitude的每个赋值,看看你是否在某个地方意外地将它设置为null(大多数IDE都有一个“查找用法”功能或类似功能,它可以帮助很多像这样的东西)。
编辑: 根据您的意见,问题在于您的活动结构。 你说你有两个活动,ActivityA包含EditTexts和包含地图的ActivityB。 Android通常一次只能打开一个Activity。其他人不是“活着”。因此,如果您打开ActivityB并想要在ActivityA中编辑可能不起作用的内容。 您可以使用的是:
- 使用startActivityForResult而不是startActivity启动活动:
startActivityForResult(intent, requestCode); //requestCode is any unique number>0 you want.
- 在ActivityA中实现onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == x) { //x is the requestCode you chose above
if(resultCode == RESULT_OK){
String latitude=data.getStringExtra("latitude");
String longitude=data.getStringExtra("longitude");
} else if (resultCode == RESULT_CANCELED) {
//ActivityB was closed before you put any results
}
}
}
- 使用ReturnMapData-Method:
进行类似的操作public void ReturnMapData(LatLng arg) {
Intent intent = this.getIntent();
intent.putExtra("latitude", String.valueOf(arg.latitude));
intent.putExtra("longitude", String.valueOf(arg.longitude));
this.setResult(RESULT_OK, intent);
}