每当我尝试在手机上测试我的应用时,应用会快速打开,显示白屏然后立即关闭。然后会显示一条提示“不幸的是,该应用程序已停止。”
我已经检查了构建路径,我确保添加了Google Play服务库。我已经尝试了几个已经解决过如何修复的线程,但没有一个正在工作。所以我把这篇文章作为最后的手段让别人手动检查。
这是我的代码:
package com.example.vendsale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.IntentSender;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
{
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
LocationClient mNewLocationClient;
private GoogleMap map;
private LatLng userLocation;
private DatabaseConnector dbConnector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkGooglePlayServicesAvailability();
dbConnector = new DatabaseConnector(getApplicationContext());
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings();
map.setMyLocationEnabled(true);
mNewLocationClient = new LocationClient(this, this, this);
}
protected void onStart() {
super.onStart();
mNewLocationClient.connect();
userLocation = new LatLng(mNewLocationClient.getLastLocation()
.getLatitude(), mNewLocationClient.getLastLocation()
.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLng(userLocation);
map.animateCamera(update);
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vendsale"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.example.vendsale.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission
android:name="com.example.vendsale.permission.MAPS_RECEIVE"
/>
<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="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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.vendsale.MainActivity"
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="MY KEY" />
</application>
</manifest>
活动主要:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/AppBaseTheme"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignRight="@+id/map"
android:onClick="onClick_Search"
android:text="@string/ButtonText"
android:typeface="normal" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="left"
android:hint="@string/message"
android:inputType="text"
android:typeface="normal"
android:layout_toLeftOf="@id/button1" />
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/editText1"
/>
</RelativeLayout>
答案 0 :(得分:0)
您已在xml中使用了supportmapfragment类。
添加此导入导入com.google.android.gms.maps.SupportMapFragment;
并将地图初始化行更改为:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
我假设您的代码中的mNewLocationClient
部分正常工作,并且那里没有nullpoin。