我的代码出了什么问题?我正在做一个地图/位置应用程序。显示地图是好的但是尝试添加setUpMap()方法/函数会导致应用程序停止。
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
的活动:
包tri.mylocapp;
//import...
public class MainActivity extends Activity implements LocationListener{
GoogleMap googlemap = null;
LatLng latlng;
LocationManager lm;
String provider;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isGooglePlay()){
setContentView(R.layout.activity_main);
setUpMap();
}
}
private boolean isGooglePlay(){
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS){
Log.d("mytag", "google ok");
return true;
}
else{
Log.d("mytag","google not ok");
Toast.makeText(this, "GooglePlay is not available", Toast.LENGTH_SHORT).show();
}
return false;
}
private void setUpMap(){
if (googlemap == null){
Log.d("mytag", "googlemap is null, making it available");
googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if (googlemap != null){
Log.d("mytag", "googlemap is not null");
googlemap.setMyLocationEnabled(true);
provider = lm.getBestProvider(new Criteria(), true);
if (provider == null){
onProviderDisabled(provider);
}
location = lm.getLastKnownLocation(provider);
if (location != null){
onLocationChanged(location);
}
}
}
}
@Override
public void onLocationChanged(Location location) {
latlng = new LatLng(location.getLatitude(), location.getLongitude());
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
LogCat(错误和mytag):
04-06 12:54:13.099: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:13.099: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 255984 bytes, got 4
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 255980 bytes, got 49152
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 206828 bytes, got 49152
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 157676 bytes, got 49152
04-06 12:54:13.103: E/SocketStream(342): readFully was waiting for 108524 bytes, got 16384
04-06 12:54:13.255: E/Trace(1750): error opening trace file: No such file or directory (2)
04-06 12:54:20.723: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:20.723: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:20.723: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:20.723: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:54:37.215: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:37.215: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:37.215: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:37.215: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:54:39.579: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:39.579: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:39.579: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:39.579: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:54:39.599: E/Trace(1769): error opening trace file: No such file or directory (2)
04-06 12:54:39.607: E/jdwp(1769): Failed sending reply to debugger: Broken pipe
04-06 12:54:41.403: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:41.403: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:41.403: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:41.403: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:54:41.679: E/BinaryDictionaryGetter(531): Could not find a dictionary pack
04-06 12:54:41.679: E/ActivityThread(531): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
04-06 12:54:41.711: E/dalvikvm(1633): Could not find class 'android.app.AppOpsManager', referenced from method box.a
04-06 12:54:43.767: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:54:43.767: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:54:43.767: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:54:43.767: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:55:57.495: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:55:57.495: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:55:57.499: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:55:57.499: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 12:55:57.531: E/Trace(1841): error opening trace file: No such file or directory (2)
04-06 12:55:57.827: E/AndroidRuntime(1841): FATAL EXCEPTION: main
04-06 12:55:57.827: E/AndroidRuntime(1841): java.lang.RuntimeException: Unable to start activity ComponentInfo{tri.mylocapp/tri.mylocapp.MainActivity}: java.lang.NullPointerException
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.os.Looper.loop(Looper.java:137)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-06 12:55:57.827: E/AndroidRuntime(1841): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 12:55:57.827: E/AndroidRuntime(1841): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 12:55:57.827: E/AndroidRuntime(1841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-06 12:55:57.827: E/AndroidRuntime(1841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-06 12:55:57.827: E/AndroidRuntime(1841): at dalvik.system.NativeStart.main(Native Method)
04-06 12:55:57.827: E/AndroidRuntime(1841): Caused by: java.lang.NullPointerException
04-06 12:55:57.827: E/AndroidRuntime(1841): at tri.mylocapp.MainActivity.setUpMap(MainActivity.java:61)
04-06 12:55:57.827: E/AndroidRuntime(1841): at tri.mylocapp.MainActivity.onCreate(MainActivity.java:35)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.Activity.performCreate(Activity.java:5008)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-06 12:55:57.827: E/AndroidRuntime(1841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-06 12:55:57.827: E/AndroidRuntime(1841): ... 11 more
04-06 12:55:57.859: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 12:55:57.859: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 12:55:57.859: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 12:55:57.859: E/SocketStream(342): readFully was waiting for 255984 bytes, got 49152
04-06 12:55:57.859: E/SocketStream(342): readFully was waiting for 206832 bytes, got 49152
04-06 13:13:01.315: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 13:13:01.315: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 13:13:01.315: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 13:13:01.315: E/SocketStream(342): readFully was waiting for 255984 bytes, got 16384
04-06 13:13:01.363: E/Trace(1874): error opening trace file: No such file or directory (2)
04-06 13:13:01.375: E/jdwp(1874): Failed sending reply to debugger: Broken pipe
04-06 13:13:01.643: E/AndroidRuntime(1874): FATAL EXCEPTION: main
04-06 13:13:01.643: E/AndroidRuntime(1874): java.lang.RuntimeException: Unable to start activity ComponentInfo{tri.mylocapp/tri.mylocapp.MainActivity}: java.lang.NullPointerException
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.os.Looper.loop(Looper.java:137)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-06 13:13:01.643: E/AndroidRuntime(1874): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:13:01.643: E/AndroidRuntime(1874): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 13:13:01.643: E/AndroidRuntime(1874): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-06 13:13:01.643: E/AndroidRuntime(1874): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-06 13:13:01.643: E/AndroidRuntime(1874): at dalvik.system.NativeStart.main(Native Method)
04-06 13:13:01.643: E/AndroidRuntime(1874): Caused by: java.lang.NullPointerException
04-06 13:13:01.643: E/AndroidRuntime(1874): at tri.mylocapp.MainActivity.setUpMap(MainActivity.java:61)
04-06 13:13:01.643: E/AndroidRuntime(1874): at tri.mylocapp.MainActivity.onCreate(MainActivity.java:35)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.Activity.performCreate(Activity.java:5008)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-06 13:13:01.643: E/AndroidRuntime(1874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-06 13:13:01.643: E/AndroidRuntime(1874): ... 11 more
04-06 13:13:01.671: E/SocketStream(342): readFully was waiting for 403440 bytes, got 49152
04-06 13:13:01.671: E/SocketStream(342): readFully was waiting for 354288 bytes, got 49152
04-06 13:13:01.671: E/SocketStream(342): readFully was waiting for 305136 bytes, got 49152
04-06 13:13:01.671: E/SocketStream(342): readFully was waiting for 255984 bytes, got 49152
04-06 13:13:01.671: E/SocketStream(342): readFully was waiting for 206832 bytes, got 49152
答案 0 :(得分:1)
在尝试将其值与任何内容进行比较之前,您需要将googlemap对象初始化为null。
即。
private void setUpMap(){
if (googlemap == null){ //in your code googlemap var is uninitialized at this point
...}}
尝试更改该行:
GoogleMap googlemap;
到
GoogleMap googlemap = null;
哦,顺便说一下;我只想说谢谢你包含你的调试信息。我在这里浏览了一些这些Android问题,我不断看到人们包含代码的情况;但不是错误信息。在我看来,这实际上相当于去一个机械车间并向机械师询问为什么你的车开得很滑,当他们要求出去看看时;你告诉他们在家里的车库里......