getIntent()返回空指针异常

问题描述 投票:2回答:2

打开活动时,下面的方法返回空指针异常。谁会知道为什么?当调用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);
    }
java android android-intent null maps
2个回答
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));
    }
  }
}

0
投票
String action = intent.getAction();
 if (action != null && action.equals("registrationComplete")) {
}else {}
© www.soinside.com 2019 - 2024. All rights reserved.