2015-04-21 2 views
2

La méthode ci-dessous renvoie une exception de pointeur NULL lorsque l'activité est ouverte. Quelqu'un sait-il pourquoi? Est-ce quelque chose à faire sans intention existante lorsque getIntent() est appelé?getIntent() renvoie une exception de pointeur nul

Toute aide serait appréciée.

Log:

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 

code:

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); 
    } 
+0

Intéressant. Comment votre "activité" est-elle démarrée? Dans des circonstances normales, l'action devrait contenir quelque chose, à moins que vous ne lanciez l'application avec un "Intent" explicite. Comme le suggère @Drakkin, vous pouvez/devriez vérifier ACTION == 'null', mais je suis surpris que ce soit le cas. –

Répondre

3

juste vérifier la nullité de votre intention et votre action, l'action par défaut est nul.

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

En fait, ACTION par défaut devrait être 'Intent.ACTION_MAIN' –