为了账号安全,请及时绑定邮箱和手机立即绑定

求大神解答,老是NullpointerException

求大神解答,老是NullpointerException

宝慕林0518688 2016-08-01 21:19:17
图片上传不了,只能文本形式。1.Logcat08-01 08:46:07.112: E/AndroidRuntime(1498): FATAL EXCEPTION: main08-01 08:46:07.112: E/AndroidRuntime(1498): Process: com.home.baidumap, PID: 149808-01 08:46:07.112: E/AndroidRuntime(1498): java.lang.RuntimeException: Unable to resume activity {com.home.baidumap/com.home.baidumap.MainActivity}: java.lang.NullPointerException08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.access$800(ActivityThread.java:135)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.os.Handler.dispatchMessage(Handler.java:102)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.os.Looper.loop(Looper.java:136)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.main(ActivityThread.java:5017)08-01 08:46:07.112: E/AndroidRuntime(1498):  at java.lang.reflect.Method.invokeNative(Native Method)08-01 08:46:07.112: E/AndroidRuntime(1498):  at java.lang.reflect.Method.invoke(Method.java:515)08-01 08:46:07.112: E/AndroidRuntime(1498):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)08-01 08:46:07.112: E/AndroidRuntime(1498):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)08-01 08:46:07.112: E/AndroidRuntime(1498):  at dalvik.system.NativeStart.main(Native Method)08-01 08:46:07.112: E/AndroidRuntime(1498): Caused by: java.lang.NullPointerException08-01 08:46:07.112: E/AndroidRuntime(1498):  at com.home.baidumap.MainActivity.onResume(MainActivity.java:50)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.Activity.performResume(Activity.java:5310)08-01 08:46:07.112: E/AndroidRuntime(1498):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)08-01 08:46:07.112: E/AndroidRuntime(1498):  ... 12 more2.源代码package com.home.baidumap;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.MapView;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.os.Build;public class MainActivity extends ActionBarActivity { private MapView mMapView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //在使用SDK各组件之前初始化context信息,传入ApplicationContext          //注意该方法要再setContentView方法之前实现          SDKInitializer.initialize(getApplicationContext());          setContentView(R.layout.activity_main);                      initView();    }    private void initView(){  //获取地图控件引用       mMapView = (MapView) findViewById(R.id.bmapView);    }        @Override      protected void onDestroy() {          super.onDestroy();          //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理          mMapView.onDestroy();      }      @Override      protected void onResume() {          super.onResume();          //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理          mMapView.onResume();          }      @Override      protected void onPause() {          super.onPause();          //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理          mMapView.onPause();          }             @Override    public boolean onCreateOptionsMenu(Menu menu) {                // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            return rootView;        }    }}
查看完整描述

5 回答

?
宝慕林0518688

TA贡献2条经验 获得超0个赞

我发现代码是我复制过去的,但是我亲自打的时候,没有onresume,onpause等的构造函数的提示和补全,我那个java proposals那个选项也勾选了,还是不行,是不是我丢失了什么包?大神。还有我那个public class MainActivity extends ActionBarActivity,不是extends Activity.这是怎么回事?

查看完整回答
反对 回复 2016-08-02
?
威神Eardwer

TA贡献2条经验 获得超1个赞

这个我之前遇到过,我是空指针异常

查看完整回答
反对 回复 2016-08-01
?
宝慕林0518688

TA贡献2条经验 获得超0个赞

setContentView(R.layout.activity_main);每个mainActivity都有的,不是吗?然后那个View rootView = inflater.inflate(R.layout.fragment_main, container, false);具体要怎么改?

查看完整回答
反对 回复 2016-08-01
  • aglla
    aglla
    对呀,是自动生成的呀,那为什么有重写了setContentView()这个方法了呢?我现在不知道你到底用哪个布局实现你的界面
?
aglla

TA贡献2条经验 获得超0个赞

可能是你的控件没获取成功,问题可能出来这两行代码setContentView(R.layout.activity_main);和 View rootView = inflater.inflate(R.layout.fragment_main, container, false);仅供参考。

查看完整回答
反对 回复 2016-08-01
  • 5 回答
  • 0 关注
  • 2024 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信