I want to call an activity from my mapfragment with a floating button and it doesn't work, the app keep crashing.
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String json_string; //ici seront stockées les information concernant les établissements
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void getBar(View view){
Log.v("myTag","button Clicked");
Intent intent = new Intent(this, DisplayFilter.class);
startActivity(intent);
}
}
DisplayFilter.java
public class DisplayFilter extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filtreview);
Log.v("myTag","vue affichée");
}
}
The logcat show this
the log
It talks about the android manifest but I don't think I should edit it right ?
I'm guessing your problem comes from this code
public void getBar(View view){
Log.v("myTag","button Clicked");
Intent intent = new Intent(this, DisplayFilter.class);
//Intent intent = new Intent(getActivity(), DisplayFilter.class);
startActivity(intent);
}
The thing is that you are in a Fragment so you can not pass this, you have to use getActivity() instead.
You need a context somehow, so you can also call view.getContext()
Whenever you want to create an Activity do these steps :
Picture is taken from : this answer
And your Manifest should look like :
<application
....
<activity1></activity1>
<activity
android:name=". DisplayFilter" />
...
</application>
Related
As of this morning I've tried to put together enough knowledge to produce a very basic app to demonstrate a concept. The idea is to display google maps, the user presses on where they want to add a marker, then a screen pops up where they can fill out more information that is then displayed when someone taps that marker.
This is what I got from the Android Studio base.
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private void setMapLongClick(final GoogleMap map) {
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.z
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Move the camera to Delft
LatLng delft = new LatLng(52.003569, 4.372987);
Float zoom = 15f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
setMapLongClick(mMap);
}
}
I tried adding this
private void setMarkerClick(final GoogleMap map) {
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
});
}
But I get and "illegal start of type" error. Am I doing this completely wrong?
Is there an easier way to add information to a marker?
This is how you should use it.
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});
Providing this as first argument when creating the Intent might not work since it refers to the OnMarkerClickListener anonymous class and not to the packageContext. Here's my suggestion, give MapsActivity.this as reference:
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});
You cannot call
this.startActivity(intent);
inside an anonymous class as startActivity is not a method of the anonymous class but of the enclosing activity. Therefore you should use
startActivity(intent);
Also use the proper anonymous implementation.
I have a small app that receives your current GPS coordinates in latitude and longitude when a "Save Location" button is pressed. Another activity is started where the map is shown and your current position is marked. On this map activity, there is a button you can press to go to another activity (let's call this the memo activity), in which there is a button to bring you back to the map activity. However, the app crashes upon pressing this button that brings you back to the map activity.
Here is the code for the main activity, map activity and the memo activity.
Main Activity
public class HomeScreen extends AppCompatActivity {
private Button button;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
private double longitude;
private double latitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
button = (Button) findViewById(R.id.button);
}
public void saveAndGo(View view) {
GPStracker g = new GPStracker(getApplicationContext());
Location l = g.getLocation();
if (l != null)
{
longitude = l.getLongitude();
latitude = l.getLatitude();
}
Intent go = new Intent(this, MapsActivity.class);
go.putExtra("longitude", longitude);
go.putExtra("latitude", latitude);
startActivity(go);
finish();
}
Map Activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Double latitude;
private Double longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
latitude = getIntent().getExtras().getDouble("latitude");
longitude = getIntent().getExtras().getDouble("longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng pos = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(pos).title("Your Location"));
mMap.setMinZoomPreference(16.0f);
mMap.setMaxZoomPreference(20.0f);
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLng(pos));
}
public void goMemos (View view) {
Intent go = new Intent(this, MemoScreen.class);
startActivity(go);
}
Memo Activity
public class MemoScreen extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memoscreen);
}
public void goMap (View view){
Intent go = new Intent(this, MapsActivity.class);
startActivity(go);
}
}
I assume it's crashing because when I try to get data from the main activity when switching back to the map activity from the memo activity, the data is null. The app runs just fine if I remove the getExtras() calls from the map activity.
How do I get around this?
Note: I would like to avoid using finish() in the memo activity since I plan to add more screens to this app, thus using that may end up in undesired behavior (i.e., returning back to the wrong activity).
you have to put simple condition that is
if(getIntent().getExtras() != null){
latitude = getIntent().getExtras().getDouble("latitude");
longitude = getIntent().getExtras().getDouble("longitude");
}
just use this in your mapsactivity
and not forgot to initialize the value of
private Double latitude=123.45;
private Double longitude=123.45;
I've been trying to setup a map using some help I got here. SO, it's very simple code but crashes. What am I doing wrong?
public class ShowDirection extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap myMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_directions);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // causes the Exception
}
#Override
public void onMapReady(final GoogleMap map) {
this.myMap = map;
myMap.setMyLocationEnabled(true);
}
Double check if your Fragment's id is actually R.id.map because it's returning null on that line. If it is not, simply replace that with the proper id.
I'm trying to make an application that allows to open new activity from a marker on the map with the method GoogleMap.OnMarkerClickListener but do not know how to use it. Does anyone help me?
Just use this:
getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
Intent i = new Intent(getActivity(), NewActivity.class);
startActivity(i);
}
});
I used this code in my custom class that extends MapFragment
I've placed it in this method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
}
When touching Info window within a marker, if changing the current screen into another screen(class or Activity), Do this below;
private GoogleMap mMap:
.....
protected void onCreate(Bundle savedInstanceState) {
.....
private void setUpMap() {
.....
mMap.setOnInfoWindowClickListener(this);
#Override
public void onInfoWindowClick(Marker marker) {
// When touch InfoWindow on the market, display another screen.
Intent intent = new Intent(this, Another.class);
startActivity(intent);
}
public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(this , check2.class);
startActivity(mi);
}
}
package com.exaple;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
}
ANDROID manifest.xml
<activity android:name=".check2" >
I am doing this but doesn't show me the message written in my other activities
#Uttam Didn't you got super not called exception?
I suppose you must call super.onCreate() in your check2.Not calling this in your check2 is causing Force Close
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
I suppose you still need to add:
#Override
public void onPause(){
super.onPause();
}
to both first and second activities.
Please change your first activity code as below
public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(Check.this , check2.class);
startActivity(mi);
}
}
& second activity code as below
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}