Domanda

Sto ottenendo un NullPointerException sollevato da un numero dei miei utenti, e non riesco a individuare quale sia il problema, viene gettato su questa linea:

((Button)findViewById(R.id.speakEnglishButton)).setText("");

Non riesco a vedere cosa c'è che non va, il pulsante esiste con quell'ID, compila bene, funziona bene sul mio emulatore e 2 dispositivi, tuttavia sto ottenendo circa 100 o meno errori vengono pubblicati nella mia console per gli sviluppatori perUtenti su questa versione.

Guardando più vicino all'Onresume:

@Override
protected void onResume()
{
    super.onResume();

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isButtonLabelsEnabled = sp.getBoolean("isButtonLabelsEnabled", false);
    if (!isButtonLabelsEnabled)
    {
        ((Button)findViewById(R.id.speakEnglishButton)).setText("");
        ((Button)findViewById(R.id.speakFrenchButton)).setText("");
        ((Button)findViewById(R.id.speakSpanishButton)).setText("");
        ((Button)findViewById(R.id.speakGermanButton)).setText("");
        ((Button)findViewById(R.id.speakItalianButton)).setText("");
    }
    else
    {
        ((Button)findViewById(R.id.speakEnglishButton)).setText(getString(R.string.btnLblEnglish));
        ((Button)findViewById(R.id.speakFrenchButton)).setText(getString(R.string.btnLblFrench));
        ((Button)findViewById(R.id.speakSpanishButton)).setText(getString(R.string.btnLblSpanish));
        ((Button)findViewById(R.id.speakGermanButton)).setText(getString(R.string.btnLblGerman));
        ((Button)findViewById(R.id.speakItalianButton)).setText(getString(R.string.btnLblItalian));
    }
}
.

In sostanza, tutto ciò che sto facendo è controllare per un booleano preferenziale salvato, e a seconda del suo valore, impostare le etichette sui pulsanti o impostarli sugli spazi vuoti.

Qualcuno può avvisare?

Grazie

È stato utile?

Soluzione

Do all of those ((Button)findViewById(R.id.speakEnglishButton)).setText(""); .... in onCreate()

Add

private Button mSpeakEngButton;
.....

As class variables, initialize them in onCreate()

public void onCreate() {

   ....
   mSpeakEngButton = ((Button)findViewById(R.id.speakEnglishButton));
}

Later in code you can modify their values as mSpeakEngButton.setWhatever()

Altri suggerimenti

I think the point is that when the user resumes the activity the layout is not set and thus a findViewById() has no 'anchor' to look for and thus returning null.

You may just move the call to setContentView() from onCreate to onResume (given that you are not referencing views in onCreate. For normal usage this makes no difference as in the Android lifecycle, onResumeis directly called after onCreate and before the user can interact with the activity.

Actually there is a bug in some applications where you visit an activity, then start a different one, perhaps leave the app for a different one, return and then return (via back button) to the first activity. Here all of a sudden you see a black screen. This is because the system "swapped" out the first activity and on returning only onResume is called, but not onCreate, so there is no chance to load the layout again there.

Most likely that's caused when

((Button)findViewById(R.id.speakEnglishButton));

returns null. This happens if such a resource cannot be found. Spontaneously one reason comes to my mind: You do not have default resources in your layout. That means: You must have at least one "default" layout, which applies for each language/orientation/... which should be placed in the folder "layout". Simply "layout".

If you keep setContentView() at onResume() instead of onCreate(), it will give inflate exception, I have tried this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top