意向结果"Unfortunately, app has stopped."



我目前正在用Google Places制作一个应用程序,实现谷歌地图。然而,每当我离开上述活动,并试图从另一个活动再次打开它时,应用程序就会突然崩溃。如有任何帮助,我们将不胜感激。

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>

主要活动.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);
        }
    });
}
}
Intent intent = new Intent(CurrentClass.this, MainActivity.class);

通过使用上面的代码,您可以使用它的最新设置。旧的设置就是您使用的。它甚至说在文档中使用上面的代码。

现在,有很多可能的崩溃,在没有看到StackTrace的情况下,我无法确定它是否真的是意图,或者它是否是代码中的某些东西在加载目标活动时导致崩溃。

相关内容

最新更新