【问题标题】:Android:MapActivity is giving NullpointerexceptionAndroid:MapActivity 给出 Nullpointerexception
【发布时间】:2013-01-29 05:43:57
【问题描述】:

当我第一次加载我的应用时,mapActivity 正在调用并且我可以看到地图,现在当我 关闭网络连接并重新打开,更改屏幕并返回 到mapactivity,地图显示为null,此时出现nullpointer异常 在onResume() 方法中新建MapUtils().drawMap(this);

@Override
protected void onResume() {
    Utils utils = new Utils();
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new ManageLocation();

    listener.setSucessCallBack(this, "setLocation");
    listener.setFailureCallBack(this, "setNoLocation");


    manager.requestLocationUpdates(
            utils.getCurrentPlaceLocationProvider(manager), 0, 0,
            listener);
    super.onResume();

    Log.d(TAG, "Constants.isDataLoadedPAB " + Constants.isMaptobeLoaded
            + " Constants.isDataLoadedPAB : " + Constants.isDataLoadedPAB);
    if (Constants.isMaptobeLoaded) {
        if (Constants.isDataLoadedPAB) {
            try {
                new MapUtils().drawMap(this);
            } catch (Exception e) {
                Log.e(TAG, "Error", e);
            }
        } else {
            if (Constants.currentLocation != null
                    && Constants.searchLocation != null) {
                if (Constants.searchResultData == null) {
                    Constants.searchResultData = new ArrayList<AttractionData>();
                }

                new MapUtils().drawMap(this);

            }
            }
    }
    ((ImageView) activity.findViewById(R.id.left)).setEnabled(true);
    ((ImageView) activity.findViewById(R.id.right)).setEnabled(true);   
    ((ImageView) findViewById(R.id.searchicon)).setEnabled(true);

    ((ImageView) activity.findViewById(R.id.facebookintegration)).setEnabled(true);
}

我试图在onResume() 中再次调用它,但仍然为空。 我很震惊,不确定我在哪里做错了。感谢任何帮助

我的日志

01-29 11:23:36.325: E/AndroidRuntime(6668): Caused by: java.lang.NullPointerException
01-29 11:23:36.325: E/AndroidRuntime(6668):     at org.appright.myneighborhood.maps.MapUtils.drawMap(MapUtils.java:59)
01-29 11:23:36.325: E/AndroidRuntime(6668):     at org.appright.myneighborhood.activity.CityAttractions.onResume(CityAttractions.java:505)
01-29 11:23:36.325: E/AndroidRuntime(6668):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
01-29 11:23:36.325: E/AndroidRuntime(6668):     at android.app.Activity.performResume(Activity.java:5280)
01-29 11:23:36.325: E/AndroidRuntime(6668):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
01-29 11:23:36.325: E/AndroidRuntime(6668):     ... 10 more

【问题讨论】:

  • 您拼写为 Success Sucess - 这与它有什么关系吗?
  • @RachelGallen 该方法只是成功而不是成功
  • 从logcat看来MapUtils().drawMap(this)中有一个null exception。能不能展示一下MapUtils().drawMap(this)的功能
  • @AnimeshSinha 这是第 13 行的 mapUtils 代码 pastebin.com/JTL25pHz 我遇到异常
  • 您的xml layout 文件中是否包含defaultmapView 命名MapView?

标签: android nullpointerexception android-mapview mapactivity


【解决方案1】:

问题出在这一行

FrameLayout topLayout = (FrameLayout) activity.findViewById(R.id.map_container);
                    topLayout.removeAllViews();

MapUtils().drawMap(this);

您正在做的是从容器中删除所有视图,即FrameLayout map_containernetwork connection 不可用时。

当网络再次出现时,您正在访问您已从布局层次结构中删除MapView,这次它不会找到defaultmapView并抛出NullPionterException

尝试注释else 块中的所有代码,只显示祝酒词。希望它会奏效。或者尝试一些不同的逻辑。

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多