我是android maps开发人员的新手。我需要将我的应用程序运行到我的设备上但是我收到错误“java.lang.RuntimeException:无法启动活动ComponentInfo {com.daniel.pencarikakus / com.daniel.pencarikakus.SplashScreen}:android.view.InflateException:Binary XML文件行#7:错误膨胀类android.widget.ImageView“
代码activity_maps.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:text="@string/map_type"
android:id="@+id/Mtype"
android:layout_gravity="right"
android:nestedScrollingEnabled="false"
android:onClick="changeType" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="338dp"
android:layout_height="539dp" android:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="@string/zoom_in"
android:id="@+id/Bzoomin"
android:onClick="onZoom" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="41dp"
android:layout_height="wrap_content"
android:text="@string/zoom_out"
android:id="@+id/Bzoomout"
android:layout_gravity="center_vertical"
android:onClick="onZoom" />
<Button
android:id="@+id/show_location"
android:layout_width="26dp"
android:layout_height="443dp"
android:text="@string/show_my_location"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
代码MapsActivity.java
package com.daniel.pencarikakus;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
private Button show_location;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
private String[] perms = {"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.INTERNET"};
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
show_location = (Button) findViewById(R.id.show_location);
textView = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
textView.setText("\n" + location.getLatitude() + " , " + location.getLongitude());
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q="+ location.getLatitude()+ "," + location.getLongitude()+ " (" + "ali" + ")"));
startActivity(intent);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if(shouldAskPermission()) {
requestPermissions(perms, 1);
}else {
configureButton();
}
}
private void configureButton() {
show_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
}
});
}
private boolean shouldAskPermission(){
return(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1 :
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
configureButton();
} else {
Toast.makeText(MapsActivity.this, "Location Denied", Toast.LENGTH_SHORT)
.show(); }
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
public void onZoom(View view) {
if (view.getId() == R.id.Bzoomin) {
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
if (view.getId() == R.id.Bzoomout) {
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
}
public void changeType(View view) {
if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.3808998, 106.8560731)).title("Kakus SPBU Pertamina HJ. Juanda Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.392380, 106.842627)).title("Kakus SPBU Pertamina Proklamasi Depok 2"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.405508, 106.851353)).title("Kakus SPBU Pertamina BBM Samping Giant Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.404099, 106.8375171)).title("Kakus SPBU Pertamina Tole Iskandar Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.399919, 106.825987)).title("Kakus SPBU Pertamina Siliwangi Samping RS. Hermina Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.384004, 106.829607)).title("Kakus SPBU Pertamina KemiriMuka Margonda Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.381064, 106.830799)).title("Kakus SPBU SHELL KemiriMuka Margonda Depok"));
mMap.addMarker(new MarkerOptions().position(new LatLng(-6.374302, 106.832806)).title("Kakus SPBU Pertamina Samping KFC Margonda Depok"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-6.398421, 106.808525), 14.9f));
}
}
Code SplashScreen.java
package com.daniel.pencarikakus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashscreen);
int splashInterval = 1000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(SplashScreen.this, MapsActivity.class);
startActivity(i);
//jeda selesai Splashscreen
this.finish();
}
private void finish() {
// TODO Auto-generated method stub
}
}, splashInterval);
}
}
Code SplashScreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="@drawable/pencarikakus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="@string/splashscreen" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar" />
</RelativeLayout>
和本代码AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.daniel.pencarikakus">
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="PencariKakus"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Splash screen -->
<activity
android:name=".SplashScreen"
android:label="PencariKakus"
android:screenOrientation="portrait"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapsActivity"
android:screenOrientation="portrait"
android:label="PencariKakus">
</activity>
<!--memasukan elemen Activity dengan atribut configChanges dan theme-->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
,最后一个是我的XIAOMI MI4i的摘要日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daniel.pencarikakus/com.daniel.pencarikakus.SplashScreen}:
android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5234)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageView
at android.view.LayoutInflater.createView(LayoutInflater.java:637)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:686)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2159)
at com.daniel.pencarikakus.SplashScreen.onCreate(SplashScreen.java:22)
at android.app.Activity.performCreate(Activity.java:5982)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:611)
... 23 more
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 154050816 byte allocation with 12669460 free bytes and 109MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.createFromResourceStream(Resources.java:2762)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2463)
at android.content.res.Resources.loadDrawable(Resources.java:2365)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:393)
at android.content.res.TypedArray.getDrawable(TypedArray.java:751)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.widget.ImageView.<init>(ImageView.java:131)
... 26 more
我从MapsActivity.java
收到错误private void configureButton() {
show_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
}
});
}
答案 0 :(得分:0)
图片尺寸太大。 您应该在SplashScreen.xml中调整图像大小。 我希望这会奏效。