java.lang.RuntimeException: 无法启动 activity ComponentInfo(android.view.InflateException:)
java.lang.RuntimeException: Unable to start activity ComponentInfo(android.view.InflateException:)
我想在我的项目中导入 Google Api 以查看 google 地图,但它显示错误 ( java.lang.RuntimeException: 无法启动 activity ComponentInfo )
我搜索了很多与这个问题相关的内容,但根据我的代码结构,没有找到与我的问题足够相关的内容
这是我的 Main Activity 命名为 (GoogleMapLocat)
public class GoogleMapLocat extends AppCompatActivity {
//map data
private static final String TAG = "HomeActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
//map data
if (isServicesOK()) {
init();
}
}
//MAP DATA
public void init() {
Button btnMap = (Button) findViewById(R.id.btnMap);
btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(GoogleMapLocat.this, MapActivity.class);
startActivity(intent);
}
});
}
public boolean isServicesOK() {
Log.d(TAG, "isServicesOK: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(GoogleMapLocat.this);
if (available == ConnectionResult.SUCCESS) {
Log.d(TAG, "Google Play Services is Working");
return true;
} else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)) {
//an error occured but we can resolve it
Log.d(TAG, "isServicesOK: an error occured but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(GoogleMapLocat.this, available, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "you can,t make map request", Toast.LENGTH_LONG).show();
}
return false;
}
}
这是我的第二个 Activity 命名为 (MapActivity)
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback{
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
//vars
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onMapReady: map is ready");
mMap = googleMap;
}
private void initMap(){
Log.d(TAG, "initMap: initializing map");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MapActivity.this);
}
private void getLocationPermission(){
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = true;
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: called.");
mLocationPermissionsGranted = false;
switch(requestCode){
case LOCATION_PERMISSION_REQUEST_CODE:{
if(grantResults.length > 0){
for(int i = 0; i < grantResults.length; i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = false;
Log.d(TAG, "onRequestPermissionsResult: permission failed");
return;
}
}
Log.d(TAG, "onRequestPermissionsResult: permission granted");
mLocationPermissionsGranted = true;
//initialize our map
initMap();
}
}
}
}
}
这里是mapactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
这是 GoogleMapLocat.xml 文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GoogleMapLocat">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Map"
android:id="@+id/btnMap"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的堆栈跟踪:
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
根据这行报错
at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)
这里是受影响的区域
setContentView(R.layout.activity_map);
因为当我点击 (MapActivity.java:37) 时,光标会在此行的开头闪烁 (setContentView(R.layout.activity_map);)
根据这行报错
Binary XML file line #6: Error inflating class fragment
这是受影响的代码块(因为我认为这是与我的错误相关的关键区域)
<fragment xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
这个片段有什么问题导致膨胀异常?
由于您使用的是 androidx 库,因此您必须使用 SupportMapFragment
migrated 从 Android 支持库到 AndroidX 库。
如果您使用此版本或更高版本。
com.google.android.gms:play-services-maps:17.0.0
如果您查看文档,它会扩展 androidx.fragment.app.Fragment
。
我想在我的项目中导入 Google Api 以查看 google 地图,但它显示错误 ( java.lang.RuntimeException: 无法启动 activity ComponentInfo )
我搜索了很多与这个问题相关的内容,但根据我的代码结构,没有找到与我的问题足够相关的内容
这是我的 Main Activity 命名为 (GoogleMapLocat)
public class GoogleMapLocat extends AppCompatActivity {
//map data
private static final String TAG = "HomeActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
//map data
if (isServicesOK()) {
init();
}
}
//MAP DATA
public void init() {
Button btnMap = (Button) findViewById(R.id.btnMap);
btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(GoogleMapLocat.this, MapActivity.class);
startActivity(intent);
}
});
}
public boolean isServicesOK() {
Log.d(TAG, "isServicesOK: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(GoogleMapLocat.this);
if (available == ConnectionResult.SUCCESS) {
Log.d(TAG, "Google Play Services is Working");
return true;
} else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)) {
//an error occured but we can resolve it
Log.d(TAG, "isServicesOK: an error occured but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(GoogleMapLocat.this, available, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "you can,t make map request", Toast.LENGTH_LONG).show();
}
return false;
}
}
这是我的第二个 Activity 命名为 (MapActivity)
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback{
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
//vars
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onMapReady: map is ready");
mMap = googleMap;
}
private void initMap(){
Log.d(TAG, "initMap: initializing map");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MapActivity.this);
}
private void getLocationPermission(){
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = true;
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: called.");
mLocationPermissionsGranted = false;
switch(requestCode){
case LOCATION_PERMISSION_REQUEST_CODE:{
if(grantResults.length > 0){
for(int i = 0; i < grantResults.length; i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = false;
Log.d(TAG, "onRequestPermissionsResult: permission failed");
return;
}
}
Log.d(TAG, "onRequestPermissionsResult: permission granted");
mLocationPermissionsGranted = true;
//initialize our map
initMap();
}
}
}
}
}
这里是mapactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
这是 GoogleMapLocat.xml 文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GoogleMapLocat">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Map"
android:id="@+id/btnMap"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的堆栈跟踪:
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.e_agriculture10/com.example.e_agriculture10.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
根据这行报错
at com.example.e_agriculture10.MapActivity.onCreate(MapActivity.java:37)
这里是受影响的区域
setContentView(R.layout.activity_map);
因为当我点击 (MapActivity.java:37) 时,光标会在此行的开头闪烁 (setContentView(R.layout.activity_map);)
根据这行报错
Binary XML file line #6: Error inflating class fragment
这是受影响的代码块(因为我认为这是与我的错误相关的关键区域)
<fragment xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
这个片段有什么问题导致膨胀异常?
由于您使用的是 androidx 库,因此您必须使用 SupportMapFragment
migrated 从 Android 支持库到 AndroidX 库。
如果您使用此版本或更高版本。
com.google.android.gms:play-services-maps:17.0.0
如果您查看文档,它会扩展 androidx.fragment.app.Fragment
。