Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.IllegalStateException:无法为android执行方法:onClick access Variables android_Java_Android_Google Maps_Location - Fatal编程技术网

java.lang.IllegalStateException:无法为android执行方法:onClick access Variables android

java.lang.IllegalStateException:无法为android执行方法:onClick access Variables android,java,android,google-maps,location,Java,Android,Google Maps,Location,我正在开发应用程序 还有一个按钮,当我点击时,它会显示纬度和经度(当前位置和选择的标记) 对于所选标记,我创建自定义信息窗口适配器 在这个类中,所选标记有纬度和经度,因此在CustomInfoWindowAdapter.java中,我为(纬度和经度)创建了GET方法 在MainActivityMaps.java中,我为按钮创建了一个方法,在这个方法中我创建了(显示当前和标记的两个方法被选中) 当前工作正常,但我在(选定标记)应用程序崩溃时遇到问题(错误) MainActivity.java //

我正在开发应用程序 还有一个按钮,当我点击时,它会显示纬度和经度(当前位置和选择的标记) 对于所选标记,我创建自定义信息窗口适配器 在这个类中,所选标记有纬度和经度,因此在
CustomInfoWindowAdapter.java
中,我为(纬度和经度)创建了
GET
方法 在
MainActivityMaps.java
中,我为按钮创建了一个方法,在这个方法中我创建了(显示当前和标记的两个方法被选中) 当前工作正常,但我在(选定标记)应用程序崩溃时遇到问题(错误)

MainActivity.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()));
}
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;
    }
}
CutomInfoWindowAdapter.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()));
}
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>

除非我遗漏了什么,否则标记总是空的

public  double getlati(){
    LatLng location = marker.getPosition();

    return location.latitude;
}

public  double getlongi(){
    LatLng location = marker.getPosition();

    return location.longitude;
}

也许我遗漏了什么,但我发现适配器中没有设置标记的位置。你在顶部声明它,但它从未设置。因此总是空的。

问题是CustomInfoWindowAdapter中的标记对象是空的,这就是引发NullPointerException的原因

public  double getlati(){
    LatLng location = marker.getPosition();

    return location.latitude;
}

public  double getlongi(){
    LatLng location = marker.getPosition();

    return location.longitude;
}
解决此问题的一种方法是在该类的构造函数中添加标记,我的意思是替换以下内容:

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); 

调用displayplace(MCurLocationMarker)时出现相同错误;你确定mCurrLocationMarker不是空的吗?不幸的是,它在Logcat中是空的(05:04:21.310 16061-16061/com.example.mostafa.parking D/null:marker是空的)。您正在尝试获取不存在的标记的坐标。无论在何处设置标记的值,都要检查代码。或者编辑你的问题并向我们展示解决了的问题我忘了放“静态”字静态私人标记;