Reputation: 31
I'm currently making an app with Google Places implementing Google Maps. However, whenever I leave the said activity, and attempt to open it again from another activity, the app suddenly crashes. Any help would be very appreciated.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="places01.cutaneous.thesis.com.places01">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BackToThis">
<intent-filter>
<action android:name="places01.cutaneous.thesis.com.places01.BackToThis" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="left blank." />
</application>
MainActivity.java
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
int PLACE_PICKER_REQUEST = 1;
LatLng newPos;
String placeName;
SupportMapFragment mapFragment;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent("places01.cutaneous.thesis.com.places01.BackToThis"));
}
});
// For displaying Google Maps
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// For displaying Google Places
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// For Google Places
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this, data);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
// setMap(mapFragment);
// getMap();
newPos = place.getLatLng();
placeName = String.format("%s", place.getName());
mMap.addMarker(new MarkerOptions().position(newPos).title(placeName));
onMapReady(mMap);
}
}
// super.onActivityResult(requestCode, resultCode, data); */
}
@Override
public void onMapReady(GoogleMap googleMap) {
// For Google Maps
mMap = googleMap;
LatLng newLatLng = newPos;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
mMap.setMyLocationEnabled(true);
if (newPos != null) {
Toast.makeText(this, newLatLng.toString(), Toast.LENGTH_LONG).show();
mMap.moveCamera(CameraUpdateFactory.newLatLng(newPos));
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onBackPressed() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
BackToThis.java
public class BackToThis extends AppCompatActivity {
private static Button button;
private static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_back_to_this);
textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("places01.cutaneous.thesis.com.places01.MainActivity");
startActivity(intent);
}
});
}
}
Upvotes: 1
Views: 2203
Reputation: 28238
Intent intent = new Intent(CurrentClass.this, MainActivity.class);
By using the above code, you use the newest setup of it. The older setup is what you used. It even says to use the above code in the documentation.
Now, there are many possible crashes and without seeing the StackTrace, I cannot determine if it actually is the intent or if it is something in the code that causes crash upon loading target activity.
Upvotes: 1