gpt4 book ai didi

java - Intent 结果到 "Unfortunately, app has stopped."

转载 作者:行者123 更新时间:2023-11-30 01:03:40 25 4
gpt4 key购买 nike

我目前正在制作一个应用程序,其中包含实现 Google map 的 Google Places。然而,每当我离开上述 Activity ,并试图从另一个 Activity 中再次打开它时,应用程序突然崩溃。任何帮助将不胜感激。

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>

主 Activity .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);

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

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

关于java - Intent 结果到 "Unfortunately, app has stopped.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39128116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com