更新如下:
单击按钮时,我启动了此地图活动。我在 logcat 中收到以下内容。我已将活动添加到清单中。我在 onCreate 中有 setContentView。我也有清单中声明的权限。布局 xml 只是一个地图片段。有人看到任何可能导致此问题的原因吗?
日志猫:
10-09 23:16:10.516: W/dalvikvm(27281): threadid=1: thread exiting with uncaught exception (group=0x41955700)
10-09 23:16:10.516: E/AndroidRuntime(27281): FATAL EXCEPTION: main
10-09 23:16:10.516: E/AndroidRuntime(27281): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.indeeditis/com.indeeditis.FinderActivity}: java.lang.NullPointerException
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.os.Looper.loop(Looper.java:137)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 23:16:10.516: E/AndroidRuntime(27281): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 23:16:10.516: E/AndroidRuntime(27281): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 23:16:10.516: E/AndroidRuntime(27281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 23:16:10.516: E/AndroidRuntime(27281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 23:16:10.516: E/AndroidRuntime(27281): at dalvik.system.NativeStart.main(Native Method)
10-09 23:16:10.516: E/AndroidRuntime(27281): Caused by: java.lang.NullPointerException
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.location.Geocoder.<init>(Geocoder.java:83)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.location.Geocoder.<init>(Geocoder.java:95)
10-09 23:16:10.516: E/AndroidRuntime(27281): at com.indeeditis.FinderActivity.<init>(FinderActivity.java:63)
10-09 23:16:10.516: E/AndroidRuntime(27281): at java.lang.Class.newInstanceImpl(Native Method)
10-09 23:16:10.516: E/AndroidRuntime(27281): at java.lang.Class.newInstance(Class.java:1130)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-09 23:16:10.516: E/AndroidRuntime(27281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-09 23:16:10.516: E/AndroidRuntime(27281): ... 11 more
活动:
public class FinderActivity extends Activity implements LocationListener {
GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
LocationManager locationmanager;
double lati;
double longi;
String nameFirst1;
List<Address> address;
Geocoder coder = new Geocoder(this);
private static final String TAG_ID = "id";
private static final String TAG_FIRSTNAME = "nameFirst";
private static final String TAG_LASTNAME = "nameLast";
private static final String TAG_EMAIL = "emailAddress";
private static final String TAG_ADDRESS = "streetAddress";
private static final String TAG_STATE = "state";
private static final String TAG_ZIPCODE ="zipCode";
private static final String TAG_COMPANY ="company";
private static final String TAG_OTHERCOMPANY ="companyTwo";
private static final String TAG_PHONE = "phone";
JSONArray contacts = null;
private static class LocationData {
private double lat;
private double longitude;
private String name;
private String other;
public LocationData(double lat, double longitude, String name, String other) {
this.lat = lat;
this.longitude = longitude;
this.name = name;
this.other = other;
}
public void setLat(double lat) {
this.lat = lat;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLat() {
return lat;
}
public double getLongitude() {
return longitude;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getOther() {
return other;
}
}
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap!= null) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomBy(17));
}
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, true);
Location location = locationmanager.getLastKnownLocation(provider);
locationmanager.requestLocationUpdates(provider, 20, 0, (LocationListener) this);
mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));
new EndpointsTask().execute(FinderActivity.this);
}
public class EndpointsTask extends AsyncTask<Context, LocationData, Long> {
private List<LocationData> locationList = new ArrayList<LocationData>();
public Long doInBackground(Context... contexts) {
Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
String apples = endpoint.listContactInfo().execute().toString();
JSONObject jObject = new JSONObject(apples);
JSONArray jsonArr = jObject.getJSONArray("items");
for(int i =0 ; i<jsonArr.length() ;i++ ){
JSONObject jsonObj1 = jsonArr.getJSONObject(i);
// Storing each json item in variable
String id = jsonObj1.getString(TAG_ID);
String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
String state1 = jsonObj1.getString(TAG_STATE);
String zip1 = jsonObj1.getString(TAG_ZIPCODE);
String phone1 = jsonObj1.getString(TAG_PHONE);
String company1 = jsonObj1.getString(TAG_COMPANY);
// String othercompany1 = jsonObj1.getString(TAG_OTHERCOMPANY);
String othercompany1 = String.valueOf(jsonObj1.optString(TAG_OTHERCOMPANY));
//test to see if made it to string
Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);
Log.d("nlltag", apples);
String whereDeyAt = streetAddress1 + " " + zip1;
address = coder.getFromLocationName(whereDeyAt,5);
Address location1 = address.get(0);
// SET LAT LNG VALUES FOR MARKER POINT
double lati = location1.getLatitude();
double longi = location1.getLongitude();
String otherinfo;
if (othercompany1==null) { otherinfo = company1 + ", " + " " + phone1;
}
else {otherinfo = company1 + ", "+ othercompany1 + " " + phone1; }
Log.d("Location", "Location:" + lati + " " + longi);
LocationData data = new LocationData(lati, longi, nameFirst1 + " " + nameLast1,otherinfo );
locationList.add(data);
publishProgress(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (long) 0;
}
public LocationData onProgressUpdate(LocationData data) {
return data;
}
protected void onPostExecute(Long result) {
for(LocationData data : locationList){
mMap.addMarker(new MarkerOptions()
.position(new LatLng(data.getLat(), data.getLongitude()))
.title(data.getName())
.snippet(data.getOther()));
}
}
}
显现:
<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.indeeditis">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18"/>
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.indeeditis.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.indeeditis.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<permission
android:name="com.indeeditis.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<!-- for card.io card scanning -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- for most things, including card.io and paypal -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Camera features -->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name="StartScreen"><intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name=".MainActivity">
</activity>
<!--
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.indeeditis"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.indeeditis"/>
</intent-filter>
</receiver>
-->
<activity android:label="@string/app_name" android:name=".FinderActivity"> </activity>
<service android:name="com.paypal.android.sdk.payments.PayPalService" android:exported="false"/>
<!-- paypal stuff -->
<activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
<activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
<activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
<activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
<activity android:name="com.paypal.android.sdk.payments.PaymentCompletedActivity" />
<activity
android:name="io.card.payment.CardIOActivity"
android:configChanges="keyboardHidden|orientation" />
<activity android:name="io.card.payment.DataEntryActivity" />
<activity android:name="Maps"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqLKpJk50JUSkm_LYO_p079oqUr3qwert"/>
</application>
</manifest>
** * ** * ***更新* ** * ** * ** * ** ** _
根据@JonSkeet,我更改了 Geocoder coder = new Geocoder(this);
到 Geocoder coder = new Geocoder(getApplicationContext());
logcat 现在显示:
10-15 22:45:50.895: E/AndroidRuntime(2798): FATAL EXCEPTION: main
10-15 22:45:50.895: E/AndroidRuntime(2798): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.indeeditis/com.indeeditis.FinderActivity}: java.lang.NullPointerException
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.os.Looper.loop(Looper.java:137)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-15 22:45:50.895: E/AndroidRuntime(2798): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 22:45:50.895: E/AndroidRuntime(2798): at java.lang.reflect.Method.invoke(Method.java:525)
10-15 22:45:50.895: E/AndroidRuntime(2798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-15 22:45:50.895: E/AndroidRuntime(2798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 22:45:50.895: E/AndroidRuntime(2798): at dalvik.system.NativeStart.main(Native Method)
10-15 22:45:50.895: E/AndroidRuntime(2798): Caused by: java.lang.NullPointerException
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
10-15 22:45:50.895: E/AndroidRuntime(2798): at com.indeeditis.FinderActivity.<init>(FinderActivity.java:65)
10-15 22:45:50.895: E/AndroidRuntime(2798): at java.lang.Class.newInstanceImpl(Native Method)
10-15 22:45:50.895: E/AndroidRuntime(2798): at java.lang.Class.newInstance(Class.java:1130)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-15 22:45:50.895: E/AndroidRuntime(2798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-15 22:45:50.895: E/AndroidRuntime(2798): ... 11 more
我仍然得到一个空指针,但它现在不适用于地理编码器。