getIntent()返回空指针异常

时间:2015-04-21 15:39:45

标签: java android android-intent null maps

以下方法在打开活动时返回空指针异常。 谁会知道为什么? 当调用getIntent()时,是否存在没有意图的事情?

任何帮助都将不胜感激。

日志:

04-21 15:38:03.237: E/AndroidRuntime(1910): Process: com.lyit.project, PID: 1910
04-21 15:38:03.237: E/AndroidRuntime(1910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lyit.project/com.lyit.project.MapScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_screen);

        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mGoogleMap = fragment.getMap();

        handleIntent(getIntent());

    }

    private void handleIntent(Intent intent){
        if(intent.getAction().equals(Intent.ACTION_SEARCH)){
            doSearch(intent.getStringExtra(SearchManager.QUERY));
        }else if(intent.getAction().equals(Intent.ACTION_VIEW)){
            getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
    }

2 个答案:

答案 0 :(得分:3)

只需检查您的意图和行动的无效性,默认操作为空。

private void handleIntent(Intent intent){
  if (intent != null && intent.getAction() != null) {
    if(intent.getAction().equals(Intent.ACTION_SEARCH)){
    doSearch(intent.getStringExtra(SearchManager.QUERY));
    }else if(intent.getAction().equals(Intent.ACTION_VIEW)){
    getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
    }
  }
}

答案 1 :(得分:0)

String action = intent.getAction();
 if (action != null && action.equals("registrationComplete")) {
}else {}