致命的例外:主要 处理:org.example.easyparking,PID:6371 java.lang.RuntimeException:无法启动活动 ComponentInfo {org.example.easyparking / org.example.easyparking.MapActivity}: android.view.InflateException:二进制XML文件行#0:二进制XML 文件行#0:错误膨胀类片段
这是我的mapactivity
package org.example.easyparking;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
public class MapActivity extends AppCompatActivity implements
OnMapReadyCallback {
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
if(googleServicesAvailable()){
setContentView(R.layout.activity_map);
initMap();
}else{
//No Google Map Layout
}
}
private void initMap() {
MapFragment mapFragment = (MapFragment) getFragmentManager().
findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}
public boolean googleServicesAvailable(){
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvailable = api.isGooglePlayServicesAvailable(this);
if(isAvailable == ConnectionResult.SUCCESS){
return true;
}else if (api.isUserResolvableError(isAvailable)){
Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
dialog.show();
}else{
Toast.makeText(this, "Can't connect", Toast.LENGTH_LONG)
.show();
}
return false;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
}
}
这是显而易见的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.easyparking">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission android:name="org.example.easyparking.permission.MAP_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="org.example.easyparking.permission.MAP_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permissions.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MapActivity" />
<activity
android:name=".SecondActivity">
</activity>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDGpV3ndPQ1Z4zf1nXi5AW1c7jGPL1La7Q"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
这是第一项活动
package org.example.easyparking;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),
"Make sure that your location is turned on",
Toast.LENGTH_LONG).show();
Intent newacti = new Intent(FirstActivity.this, MapActivity.class);
startActivity(newacti);
}
});
Button button_parking_owner = (Button) this.findViewById(R.id.button2);
button_parking_owner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent newacti = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(newacti);
}
});
}
}
答案 0 :(得分:0)
在MapActivity
中,您正在检查googleServicesAvailable()
是否可用,如果没有,则表示您未提供要呈现的默认内容
if(googleServicesAvailable()){
setContentView(R.layout.activity_map);
initMap();
}else{
//No Google Map Layout
}
尝试创建一个基本布局来呈现类似 No Google Map 的内容 即你应该有类似
的东西if(googleServicesAvailable()){
setContentView(R.layout.activity_map);
initMap();
}else{
//No Google Map Layout
setContentView(R.layout.no_activity_map);
}
然后创建no_activity_map
布局