我正在使用 Google Maps v2 API 构建一个 GPS 应用程序。该 Activity 有 SupportMapFragment 和一些文本字符串。当我在我的 Android 智能手机上运行该应用程序时,它崩溃了。我不明白 LogCat。源代码如下。
医护人员.java
package ie.itsligo.medication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
public class Paramedic extends FragmentActivity {
TextView txtID;
TextView txtFName;
TextView txtLName;
TextView txtMedInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paramedic);
txtID = (TextView)findViewById(R.id.txtID);
txtFName = (TextView)findViewById(R.id.txtFName);
txtLName = (TextView)findViewById(R.id.txtLName);
txtMedInfo = (TextView)findViewById(R.id.txtMedInfo);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.paramedic, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_paramedic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Patient Location"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="231dp"
android:layout_margin="5dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Patient Details"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Patient ID:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txtFName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="First Name:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txtLName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Last Name:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txtMedInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Medical Information:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ie.itsligo.medication"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="ie.itsligo.medication.permission.MAPS_RECIEVE" android:protectionLevel="signature"/>
<uses-permission android:name="ie.itsligo.medication.permission.MAPS_RECIEVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Paramedic"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value=""/>
</application>
</manifest>
日志猫
02-15 13:12:30.549: D/dalvikvm(31488): GC_CONCURRENT freed 120K, 10% free 10493K/11655K, paused 2ms+3ms, total 31ms
02-15 13:12:30.549: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 8ms
02-15 13:12:30.909: D/dalvikvm(31488): GC_CONCURRENT freed 136K, 11% free 10821K/12039K, paused 6ms+255ms, total 291ms
02-15 13:12:30.979: I/Google Maps Android API(31488): Google Play services client version: 6587000
02-15 13:12:30.989: I/Google Maps Android API(31488): Google Play services package version: 6599034
02-15 13:12:31.299: D/dalvikvm(31488): GC_CONCURRENT freed 324K, 11% free 11375K/12743K, paused 13ms+3ms, total 53ms
02-15 13:12:31.299: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 16ms
02-15 13:12:31.429: D/dalvikvm(31488): GC_FOR_ALLOC freed 966K, 15% free 11888K/13959K, paused 38ms, total 38ms
02-15 13:12:32.090: D/dalvikvm(31488): GC_CONCURRENT freed 1123K, 15% free 13363K/15559K, paused 3ms+28ms, total 146ms
02-15 13:12:32.090: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 81ms
02-15 13:12:32.661: D/dalvikvm(31488): GC_CONCURRENT freed 691K, 11% free 14087K/15815K, paused 13ms+10ms, total 131ms
02-15 13:12:32.661: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 39ms
02-15 13:12:33.131: W/ActivityThread(31488): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
02-15 13:12:33.151: D/AbsListView(31488): Get MotionRecognitionManager
02-15 13:12:34.012: D/dalvikvm(31488): GC_CONCURRENT freed 1344K, 15% free 14394K/16775K, paused 28ms+24ms, total 215ms
02-15 13:12:34.012: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 85ms
02-15 13:12:34.032: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 90ms
02-15 13:12:34.032: D/dalvikvm(31488): WAIT_FOR_CONCURRENT_GC blocked 79ms
02-15 13:12:34.032: E/Google Maps Android API(31488): Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
02-15 13:12:34.042: E/Google Maps Android API(31488): In the Google Developer Console (https://console.developers.google.com)
02-15 13:12:34.042: E/Google Maps Android API(31488): Ensure that the "Google Maps Android API v2" is enabled.
02-15 13:12:34.042: E/Google Maps Android API(31488): Ensure that the following Android Key exists:
02-15 13:12:34.042: E/Google Maps Android API(31488): API Key: My API Key
02-15 13:12:34.042: E/Google Maps Android API(31488): Android Application (<cert_fingerprint>;<package_name>):
My Debug Certificate
02-15 13:12:34.062: I/Google Maps Android API(31488): Failed to contact Google servers. Another attempt will be made when connectivity is established.
02-15 13:12:34.553: D/libEGL(31488): loaded /system/lib/egl/libEGL_mali.so
02-15 13:12:34.593: D/libEGL(31488): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-15 13:12:34.593: D/libEGL(31488): loaded /system/lib/egl/libGLESv2_mali.so
02-15 13:12:34.693: D/OpenGLRenderer(31488): Enabling debug mode 0
02-15 13:12:34.833: I/libblt_hw(31488): Library opened (handle = 0, fd = 47)
02-15 13:12:35.253: D/dalvikvm(31488): GC_FOR_ALLOC freed 965K, 15% free 14586K/17159K, paused 202ms, total 205ms
02-15 13:12:39.347: D/dalvikvm(31488): GC_CONCURRENT freed 1651K, 16% free 14607K/17351K, paused 18ms+5ms, total 138ms
02-15 13:12:49.567: E/Google Maps Android API(31488): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
02-15 13:12:49.707: D/dalvikvm(31488): GC_CONCURRENT freed 1715K, 17% free 14605K/17415K, paused 18ms+9ms, total 187ms
最佳答案
与您在程序中使用的手机相比,您的手机的播放版本较低。我建议您使用真实设备(更新设备的播放版本)。如果不是真实设备,请尝试使用 GenyMotion 模拟器。
在AndroidManifest.xml 中添加
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
您的代码应该如下所示:
<permission android:name="ie.itsligo.medication.permission.MAPS_RECIEVE" android:protectionLevel="signature"/>
<uses-permission android:name="ie.itsligo.medication.permission.MAPS_RECIEVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Paramedic"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value=""/>
</application>
</manifest>
关于android - Eclipse Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28519675/