2015-11-17 7 views
5

Nota: una soluzione limitativa da another, similar SO question's answer ha funzionato per me, ma sono interessato a trovare una soluzione reale. La soluzione era quella di aggiungere questo per la mia attività:Android NullPointerException - Il parametro `view` di Spinner onItemSelected è nullo dopo la rotazione

@Override 
protected void onSaveInstanceState(Bundle outState) { /* do nothing */ } 

Ma mi vorrebbe ancora di lasciare aperta la questione nella speranza di trovare una soluzione migliore.


mia app si blocca quando ruota sulla più recente Android (Marshmallow, Lecca-lecca), ma funziona su KitKat. Nel metodo Spinner onClick, ottengo il primo figlio del genitore, che è la prima finestra di testo nell'elenco a discesa (aka spinner). Funziona bene quando non lo ruoto. Inoltre, quando commento quella linea che lancia NullPointerException, funziona anche bene. Quindi questa è l'unica causa del problema.

So che è Null, ma non capisco perché o come risolverlo? Inoltre, tieni presente che non posso utilizzare XML per questo, perché il colore del testo è noto solo dinamicamente in fase di runtime. Inoltre, voglio che l'API minima sia 15.

Ho aggiunto un po 'di codice di debug e ho scoperto che prima della rotazione, i parametri parent e view non sono entrambi nulli. Ma dopo la rotazione, view è nullo. (Ma parent non è ancora null).

Vedere ****** dalla linea che ha l'eccezione puntatore nullo:

private void setUpSpinner(int accentColor, final int backgroundColor, Toolbar toolbar) 
{ 
    Spinner spinner = (Spinner) findViewById(R.id.spinner); 

    //Get rid of the normal toolbar's title, because the spinner is replacing the title. 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    //Set the choices on the spinner by setting the adapter. 
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, 
      accentColor, backgroundColor)); 

    //Set the listener for when each option is clicked. 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
     { 
      Log.e("ITEM SELECTED", "SPINNER" + Integer.toString(position)); 

      //This is necessary to set the color of the "action bar title." (Really, this is just 
      //changing the text color of the spinner when it is at rest; or changing the selected 
      //option's color.) 
****************((TextView) view).setTextColor(backgroundColor); 

      //Change the contents of the DealPage depending on what option was selected in the spinner. 
      // CODE OMITTED 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) 
     { 
     } 
    }); 
} 

spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@android:id/text1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceListItemSmall" 
      android:gravity="center_vertical" 
      android:padding="5dp" 
      android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 

metodo OnCreate:

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     context = this; 
     RealmDatabase.setRealmInstance(this); 

     //----------- UNPACK EXTRAS ----------- 
     String date = getIntent().getExtras().getString(KeyStrings.EXTRA_DATE); 

     //---------------- PREREQUISITE INITIALIZATION ---------- 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_deal_page); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_with_spinner); 
     setSupportActionBar(toolbar); 

     //------------ Enable "UP" navigation --------------- 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     getDataFromDatabase(date); 

     //----------------- THEMES AND COLORS ------------------ 

     //Set up colors 
     final int backgroundColor = this.deal.getTheme().getBackgroundColor().getColor(); 
     final int accentColor = this.deal.getTheme().getAccentColor().getColor(); 
     String themeForeground = this.deal.getTheme().getForeground(); 
     final int foreground = generateForegroundColor(themeForeground); 
     final String foregroundWebView = generateForegroundWebViewString(themeForeground); // This is necessary because HTML does not have an alpha channel. 

     //Set toolbar colors 
     toolbar.setBackgroundColor(accentColor); 
     toolbar.setTitleTextColor(backgroundColor); 

     //Set Page Background Color 
     RelativeLayout dealPageBackground = (RelativeLayout) findViewById(R.id.deal_page_background); 
     dealPageBackground.setBackgroundColor(backgroundColor); 

     //----------- INITIALIZE THE ACTUAL DEAL PAGE STUFF ---------------- 

     //Title 
     TextView title = (TextView) findViewById(R.id.title); 
     title.setText(this.deal.getTitle()); 
     title.setTextColor(foreground); 

     //Price 
     TextView price = (TextView) findViewById(R.id.price); 
     NumberFormat fmt = NumberFormat.getCurrencyInstance(); 
     price.setText(fmt.format(this.deal.getItems().first().getPrice())); 
     price.setTextColor(foreground); 

     //ViewInBrowser 
     setUpViewInBrowserButton(backgroundColor, accentColor); 

     AndDown andDown = new AndDown(); 

     //Set up "linkColorHTML" 
     String linkColorHTML = generateLinkColorHTML(accentColor); 

     //Features 
     setUpFeaturesView(andDown, backgroundColor, linkColorHTML, foregroundWebView); 

     //More Specs button 
     setUpMoreSpecsButton(backgroundColor, foreground, (Spinner) findViewById(R.id.spinner)); 

     //Story Title 
     TextView storyTitle = (TextView) findViewById(R.id.story_title); 
     storyTitle.setText(this.deal.getStory().getTitle()); 
     storyTitle.setTextColor(accentColor); 

     //Story Body 
     setUpStoryBody(andDown, backgroundColor, linkColorHTML, foregroundWebView); 

     //Specs Title 
     TextView specsTitle = (TextView) findViewById(R.id.specs_title); 
     specsTitle.setText(this.deal.getTitle()); 
     specsTitle.setTextColor(accentColor); 

     //Specs 
     setUpSpecificationsView(andDown, backgroundColor, linkColorHTML, foregroundWebView); 

     //Set up ViewPager 
     ViewPager viewPager = (ViewPager) findViewById(R.id.photos_view_pager); 
     viewPager.setAdapter(new PhotoPagerAdapter(this, this.deal.getPhotos())); 

     //Set up spinner 
     setUpSpinner(accentColor, backgroundColor, toolbar); 

     //Set up poll title 
     TextView pollTitle = (TextView) findViewById(R.id.poll_title); 
     pollTitle.setText(this.poll.getTitle()); 
     pollTitle.setTextColor(accentColor); 

     //Set up poll view-forum-topic-in-browser button 
     setUpViewForumTopicInBrowserButton(backgroundColor, accentColor); 

     //Set up poll 
     setUpPoll(foreground, accentColor); 

     //Set up video title 
     TextView videoTitle = (TextView) findViewById(R.id.video_title); 
     videoTitle.setText(this.video.getTitle()); 
     videoTitle.setTextColor(accentColor); 

     //Set up video view-forum-topic-in-browser button 
     setUpViewVideoForumTopicInBrowserButton(backgroundColor, accentColor); 

     //Set up youtube video 
     setUpYoutubeVideo(); 


    } 

Log :

11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime: FATAL EXCEPTION: main 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime: Process: com.example.meh, PID: 6507 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at com.example.meh.Deal.DealPage$6.onItemSelected(DealPage.java:480) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.AdapterView.fireOnSelected(AdapterView.java:897) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.AdapterView.selectionChanged(AdapterView.java:884) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1047) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.AdapterView.handleDataChanged(AdapterView.java:1027) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:184) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.Spinner.onMeasure(Spinner.java:507) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:410) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.support.v7.widget.Toolbar.measureChildCollapseMargins(Toolbar.java:1225) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.support.v7.widget.Toolbar.onMeasure(Toolbar.java:1333) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.View.measure(View.java:17430) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1767) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.Choreographer.doFrame(Choreographer.java:550) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:739) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:95) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5221) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
11-16 18:06:45.879 6507-6507/com.example.meh E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)f 

risposta

1
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 

adapterView si riferisce a Spinner, non all'elemento su cui si è fatto clic, si dovrebbe invece eseguire il parametro "vista".


@Rock Lee Dopo ruotato la disposizione sarebbe di aggiornamento, si dovrebbe resetup il filatore in qualche funzione di callback come onConfigurationChanged (Configurazione newconfig), Android listview disappears after screen rotation

+0

Ho modificato il codice e ho ricevuto lo stesso errore. –

0

è possibile utilizzare il metodo di override onSaveInstanceState() e onRestoreInstanceState(). o per smettere di chiamare onCreate() sulla rotazione dello schermo basta aggiungere questa riga nel tuo manifest xml android: configChanges = "keyboardHidden | orientation"

nota: la tua classe personalizzata deve implementare l'esempio Parcelable di seguito.

@Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putParcelable("obj", myClass); 
    } 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onRestoreInstanceState(savedInstanceState); 
myClass=savedInstanceState.getParcelable("obj")); 
} 

public class MyClass implements Parcelable { 
    private int mData; 

public int describeContents() { 
    return 0; 
} 

/** save object in parcel */ 
public void writeToParcel(Parcel out, int flags) { 
    out.writeInt(mData); 
} 

public static final Parcelable.Creator<MyParcelable> CREATOR 
     = new Parcelable.Creator<MyParcelable>() { 
    public MyParcelable createFromParcel(Parcel in) { 
     return new MyParcelable(in); 
    } 

    public MyParcelable[] newArray(int size) { 
     return new MyParcelable[size]; 
    } 
}; 

/** recreate object from parcel */ 
private MyParcelable(Parcel in) { 
    mData = in.readInt(); 
} 


} 
+0

se non funziona fammi sapere –

+0

Grazie. Funziona, ma potrebbe ancora causare altri problemi secondo questa risposta SO: http://stackoverflow.com/a/7990543/2423194 –

+0

sì, ha ragione. non causerà nessun altro problema.intendeva dire che se vuoi gestire qualsiasi cosa a rotazione non devi usarlo altrimenti puoi usarlo. se si desidera programmare in modo predefinito, utilizzare onConfigurationChanged –