猿问

求大神解答,老是NullpointerException

图片上传不了,只能文本形式。

1.Logcat

08-01 08:46:07.112: E/AndroidRuntime(1498): FATAL EXCEPTION: main
08-01 08:46:07.112: E/AndroidRuntime(1498): Process: com.home.baidumap, PID: 1498
08-01 08:46:07.112: E/AndroidRuntime(1498): java.lang.RuntimeException: Unable to resume activity {com.home.baidumap/com.home.baidumap.MainActivity}: java.lang.NullPointerException
08-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.NullPointerException
08-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 more

2.源代码

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;
        }
    }


}


宝慕林0518688
浏览 2025回答 5
5回答

宝慕林0518688

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

威神Eardwer

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

宝慕林0518688

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

aglla

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

相关分类

Android
Java
我要回答