这是我的MainActivity.java代码
package com.example.com.draft1;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.KeyEvent;
import com.google.android.maps.MapController;
public class MainActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
String coordinates[]={"1.352566007","103.78921587"};
double lat=Double.parseDouble(coordinates[0]);
double lng=Double.parseDouble(coordinates[1]);
@Override
public void onCreate(Bundle savedInstanceState) {
p=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
mapView=(MapView)findViewById(R.id.mapView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mc.setCenter(p);
//mc.setZoom(13);
//mapView.invalidate();
//myMapController.setCenter(new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed()
{
//adssad
return false;
}
}
现在,上面的代码导致一个力量关闭。
以下是上述的LogCat详细信息: 09-23 16:37:38.494:D / dalvikvm(18710):GC_CONCURRENT释放244K,41%自由3354K / 5639K,外部0K / 0K,暂停3ms + 4ms 09-23 16:37:38.764:D / dalvikvm(18710):GC_CONCURRENT释放530K,44%空闲3378K / 5959K,外部0K / 0K,暂停2ms + 7ms 09-23 16:37:38.894:D / dalvikvm(18710):GC_CONCURRENT释放376K,42%免费3513K / 5959K,外部0K / 0K,暂停3ms + 4ms 09-23 16:37:39.014:D / dalvikvm(18710):GC_CONCURRENT释放510K,43%免费3523K / 6087K,外部0K / 0K,暂停3ms + 4ms 09-23 16:37:39.134:D / dalvikvm(18710):GC_CONCURRENT释放334K,40%空闲3700K / 6087K,外部0K / 0K,暂停3ms + 4ms 09-23 16:37:39.194:I / HtcTextSelection(18710):( sendOnTextChanged)stopTextSelectionMode 09-23 16:37:39.244:D / dalvikvm(18710):GC_EXTERNAL_ALLOC释放698K,47%自由3319K / 6151K,外部0K / 0K,暂停45ms 09-23 16:37:39.264:D / AndroidRuntime(18710):关闭VM 09-23 16:37:39.264:W / dalvikvm(18710):threadid = 1:线程退出时未捕获异常(group = 0x400dc5a0) 09-23 16:37:39.264:E / AndroidRuntime(18710):致命异常:主要 09-23 16:37:39.264:E / AndroidRuntime(18710):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.com.draft1 / com.example.com.draft1.MainActivity}:java.lang 。空指针异常 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread.access $ 1500(ActivityThread.java:135) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.os.Handler.dispatchMessage(Handler.java:99) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.os.Looper.loop(Looper.java:150) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread.main(ActivityThread.java:4389) 09-23 16:37:39.264:E / AndroidRuntime(18710):at java.lang.reflect.Method.invokeNative(Native Method) 09-23 16:37:39.264:E / AndroidRuntime(18710):at java.lang.reflect.Method.invoke(Method.java:507) 09-23 16:37:39.264:E / AndroidRuntime(18710):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849) 09-23 16:37:39.264:E / AndroidRuntime(18710):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 09-23 16:37:39.264:E / AndroidRuntime(18710):at dalvik.system.NativeStart.main(Native Method) 09-23 16:37:39.264:E / AndroidRuntime(18710):引起:java.lang.NullPointerException 09-23 16:37:39.264:E / AndroidRuntime(18710):at com.example.com.draft1.MainActivity.onCreate(MainActivity.java:40) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 09-23 16:37:39.264:E / AndroidRuntime(18710):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 09-23 16:37:39.264:E / AndroidRuntime(18710):... 11更多
因此,当我评论.setCenter()语句时,该应用程序运行正常。但是,在取消注释时,它强制关闭
关于为什么会发生这种情况的任何线索?
感谢您的所有帮助
答案 0 :(得分:0)
非常愚蠢的错误。没有初始化mc(MapController)
我的不好