>我正在开发应用程序 还有按钮,当我点击时,它将显示纬度和经度(当前位置和所选标记( 对于选定的标记,我创建自定义信息窗口适配器 在此类中,所选标记有纬度和经度,因此CustomInfoWindowAdapter.java
我创建了GET
方法(纬度和经度( 在MainActivityMaps.java
我为按钮创建了方法,并在我创建了此方法中(选择了两种显示当前和标记的方法( 当前工作良好,但我在(标记已选择(应用程序崩溃时遇到问题(错误(
主活动.java
// button onclick call to show latitude and longitude as a toast for test
public void showcurrentlocationattost(View view){
displaytestcurrent(mLastLocation);
displayplace(mCurrLocationMarker); // mCurrLocationMarker -> just name
}
public void displaytestcurrent(Location location){
LatLng latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
String text = (String.valueOf("latitude = "+latLngcurrent.latitude+" , "+"longitude = "+ latLngcurrent.longitude));
Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
}
public void displayplace(Marker marker){
Context context = getApplicationContext();
CustomInfoWindowAdapter op = new CustomInfoWindowAdapter(context); // access getlati for latitude and getlongi //for longitude From this class //CutomInfoWindowAdapter.java
String text = (String.valueOf("latitude = "+op.getlati()+" , "+"longitude = "+ op.getlongi()));
Toast.makeText(getApplicationContext(), text,Toast.LENGTH_SHORT).show();
// for test
Log.d("testL", String.valueOf(op.getlati()));
}
CutomInfoWindowAdapter.java
package com.example.mostafa.parking;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
private Marker marker;
private final View mWindow;
private Context mContext;
double lati=0.0;
double longi=0.0 ;
String locat;
public CustomInfoWindowAdapter(Context context){
mContext = context;
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
}
private double rendomWindowtext(Marker marker, View view){
LatLng location = marker.getPosition();
String title = marker.getTitle();
TextView tvTitle = (TextView) view.findViewById(R.id.title);
TextView tvLocat = (TextView) view.findViewById(R.id.locat);
lati = location.latitude;
longi = location.longitude;
if (!title.equals("")){
tvTitle.setText(title);
}
if (!location.equals("")){
tvLocat.setText(String.valueOf(lati));
}
Log.d("lati", String.valueOf(lati));
Log.d("longi", String.valueOf(longi));
locat= String.valueOf(location);
Log.d("Location", String.valueOf(location.latitude));
String text = (String.valueOf("latitude = "+lati+" , "+"longitude = "+ longi));
Toast.makeText(mContext.getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
return lati;
}
public double getlati(){
LatLng location = marker.getPosition();
return location.latitude;
}
public double getlongi(){
LatLng location = marker.getPosition();
return location.longitude;
}
@Override
public View getInfoWindow(Marker marker) {
rendomWindowtext(marker,mWindow);
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
rendomWindowtext(marker,mWindow);
return mWindow;
}
}
.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityMaps" />
<--onClick="showcurrentlocationattost -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direction"
android:onClick="showcurrentlocationattost"/>
</RelativeLayout>
错误
Process: com.example.mostafa.parking, PID: 5750
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4700)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.Marker.getPosition()' on a null object reference
at com.example.mostafa.parking.CustomInfoWindowAdapter.getlati(CustomInfoWindowAdapter.java:62)
at com.example.mostafa.parking.MainActivityMaps.displayplace(MainActivityMaps.java:334)
at com.example.mostafa.parking.MainActivityMaps.showcurrentlocationattost(MainActivityMaps.java:306)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
除非我错过了什么,否则标记始终为空。
public double getlati(){
LatLng location = marker.getPosition();
return location.latitude;
}
public double getlongi(){
LatLng location = marker.getPosition();
return location.longitude;
}
也许我错过了一些东西,但我没有看到在适配器中设置标记的位置。你在顶部声明它,但它永远不会设置。因此始终为空。
问题是CustomInfoWindowAdapter中的标记对象是 NULL,这就是抛出 NullPointerException 的原因。
解决它的一种方法是在该类的构造函数中添加标记,我的意思是替换它:
public CustomInfoWindowAdapter(Context context){
mContext = context;
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
}
有了这个:
public CustomInfoWindowAdapter(Context context, Marker marker){
mContext = context;
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
this.marker = marker;
}
并在displayplace中创建实例时传递标记,如下所示:
CustomInfoWindowAdapter op = new CustomInfoWindowAdapter(context, marker);