People,
I am trying to send users the current latitude and longitude using the click button in MainActivity for MapFragment (Fragment). I use the interface from the fragment implemented in MainActivity, but when I try to use the method from Fragment onButtonClick, I get
in android.app.ActivityThread.main (ActivityThread.java:5221) in java.lang.reflect.Method.invoke (native method) in java.lang.reflect.Method.invoke (Method.javahaps72) at com.android .internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694) Called: java.lang.NullPointerException: attempt to call the virtual method 'void com.xtechkid.blah.threadingapplication.MapDemoActivity.setupCurrlocation (com.google.android.gms.maps.model.LatLng) 'on the null object link at com.xtechkid.sudheej.threadingapplication.MainActivity.findLocation (MainActivity.java:67)
My code is below
Here is a fragment of the map
public class MapDemoActivity extends Fragment {
final LatLng MU = new LatLng(40.8874245, 67.0200729);
private GoogleMap mMap;
CommInter mCallback;
public interface CommInter {
public void communicatesWithActivity(double lati,double longi);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Activity a;
a=(Activity) context;
try{
mCallback = (CommInter) a;
}catch (ClassCastException e){
throw new ClassCastException(a.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
return view;
}
public void setupCurrlocation(LatLng MUSEUM) {
makeText(getContext(), "I am inside the function ", Toast.LENGTH_LONG).show();
System.out.println("I am inside the function");
}
}
Code from the main activity
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends AppCompatActivity implements MapDemoActivity.CommInter {
private TextView txtLocation;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void communicatesWithActivity(double lati, double longi) {
MapDemoActivity mapFrag = (MapDemoActivity)getSupportFragmentManager().findFragmentById(R.id.map);
final LatLng MUSEU = new LatLng(38.8874245, 77.0200729);
mapFrag.setupCurrlocation(MUSEU);
}
public void findLocation(View v) {
txtLocation = (TextView) findViewById(R.id.textViewLocation);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean isLocEnabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isLocEnabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
GPSTracker gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
txtLocation.setText("Lat: " + latitude + "Long: " + longitude);
communicatesWithActivity(latitude,long);
}else{
gps.showSettingsAlert();
}
}
}