Android spinner es nulo

Aquí está mi actividad principal. Mi hiladora es nula aunque ya se ha inicializado. Mi aplicación se bloquea cuando se accede a cualquiera de los elementos de diseño

Y por la vida de mí no puedo entender por qué. He pasado bastante tiempo en esto, investigado un montón de mensajes en línea para una solución y probado una serie de soluciones. Nada ha funcionado hasta ahora. ¿Algunas ideas?

Además, ignore el código de creación / ordenación de la fecha. Es muy complicado, ya tengo una forma más limpia de hacerlo, pero no lo implementaré hasta que tenga esta parte fija.

Nota: Los identificadores son todos correctos en mis diseños, estoy muy seguro de que no es el problema.

La clase pública RADIO_Live extiende FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; View fragviewLive; View fragviewPod; private int mYear, mMonth, mDay, cYear, cMonth, cDay; private TextView mCurrentDate; private TextView mSelectedDate; private TextView TextCurrentTime; private TextView TextTotalTime; boolean firstRUN = true; SeekBar Podseekbar; int myProgress = 0; int podMax = 0; int CurrentPosition = 0; boolean playerPODon = false; // variable to keep track if live stream player and podcast player is on or off and if podacast player is paused boolean playerPODon1st = true; String mMonthName, cMonthName, cMonthFullName; String podFileTest, podPlayName, SelectedHour, SelectedDate = null; String showdate, HourShowText = null; String[] podDates = new String[14]; String[] podDatesName = new String[14]; int testDay = 0; int dayOfWeek; private final Handler UIhandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); //context.startService(serviceIntent); startService(new Intent(this, RADIO_Service.class)); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); fragviewPod = getLayoutInflater().inflate(R.layout.podcasts, null); // Capture the textview elements mCurrentDate = (TextView) fragviewPod.findViewById(R.id.currentdateDisplay); mSelectedDate = (TextView) fragviewPod.findViewById(R.id.selecteddateDisplay); TextTotalTime = (TextView) fragviewPod.findViewById(R.id.TotalTimeText); TextCurrentTime = (TextView) fragviewPod.findViewById(R.id.CurrentTimeText); // Retrieve the current date Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); IntentFilter filt = new IntentFilter("fromService"); RADIO_Live.this.registerReceiver(br, filt); IntentFilter Podfilt = new IntentFilter("fromServicePod"); RADIO_Live.this.registerReceiver(PODbr, Podfilt); actBroadcast("null", "whatsplaying"); ArrayList<CharSequence> dateadapterarray = new ArrayList<CharSequence>(); int ArrayMarker = 0; for (int podtest = 1; podtest < 15; podtest++) { MakeDateList(podtest); if (dayOfWeek != (1|7)) { podFileTest = cMonthName + " " + cDay + ", " + cYear; podDatesName[ArrayMarker] = podFileTest; String cYearTemp = cYear + ""; String cMonthTemp = cMonth + ""; if (cMonth >= 10) { } else { cMonthTemp = cMonthTemp.replace("0", ""); cMonthTemp = "0" + cMonthTemp; } if (cDay > 9) { podDates[ArrayMarker] = (cMonthTemp + "" + cDay + "" + cYearTemp.replace("20", "")); } if (cDay <= 9) { podDates[ArrayMarker] = (cMonthTemp + "" + "0" + cDay + "" + cYearTemp.replace("20", "")); } ArrayMarker++; } if (dayOfWeek == (1|7) && (ArrayMarker > 0)) { ArrayMarker--; } } for (int buildtext = 0; buildtext < 14; buildtext++) { dateadapterarray.add(podDatesName[buildtext]); } fragviewPod = getLayoutInflater().inflate(R.layout.podcasts, null); Spinner datespinner = (Spinner) fragviewPod.findViewById(R.id.spinnerdate); Spinner hourspinner = (Spinner) fragviewPod.findViewById(R.id.spinnerhour); //Spinner hourspinner = (Spinner) mViewPager.findViewById(R.id.spinnerhour); ArrayAdapter<CharSequence> dateadapter = new ArrayAdapter<CharSequence>(RADIO_Live.this, android.R.layout.simple_spinner_item, dateadapterarray); dateadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); ArrayAdapter<CharSequence> houradapter = new ArrayAdapter<CharSequence>(RADIO_Live.this, android.R.layout.simple_spinner_item, R.array.hour_array); houradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); datespinner.setAdapter(dateadapter); hourspinner.setAdapter(houradapter); // Display the current date under "Today's Date" at the top updateCurrentDate(); datespinner.setOnItemSelectedListener(new DateOnItemSelectedListener()); hourspinner.setOnItemSelectedListener(new HourOnItemSelectedListener()); Button livebutton = (Button) fragviewLive.findViewById(R.id.button_streamstartstop); //livebutton.setOnClickListener((View.OnClickListener) this); Button podplaybutton = (Button)fragviewPod.findViewById(R.id.button_pod_play); //podplaybutton.setOnClickListener((View.OnClickListener) this); } 

Actualización: Aquí es donde las opiniones se infla. Intenté inflar las dos vistas en sus propios LinearLayouts estáticos, pero tampoco funciona.

Así que al comienzo de la actividad principal:

 static LinearLayout livelayout; static LinearLayout podlayout; 

Y luego los fragmentos de código. Literalmente es el código de una aplicación predeterminada vacía de Android Studio con mis cosas rellenas:

 public static class DummySectionFragment extends Fragment { public DummySectionFragment() { } public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER); Bundle args = getArguments(); int test = args.getInt(ARG_SECTION_NUMBER); livelayout = (LinearLayout)inflater.inflate(R.layout.live, null); podlayout = (LinearLayout)inflater.inflate(R.layout.podcasts, null); inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resID = 0; switch (test) { case 1: resID = R.layout.live; break; case 2: resID = R.layout.podcasts; break; } //textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); View fragview = inflater.inflate(resID, null); return fragview; } } 

Así que para aclarar, tengo un main.xml que es sólo el encabezado y el adaptador de página y luego un live.xml y un podcast.xml.

Main.xml:

 <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".RADIO_Live" > <!-- This title strip will display the currently visible page title, as well as the page titles for adjacent pages. --> <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:textColor="#fff" android:paddingTop="4dp" android:paddingBottom="4dp" /> </android.support.v4.view.ViewPager> 

Live.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="vertical" > <LinearLayout android:id="@+id/tab1.1" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15sp" android:layout_marginBottom="10sp" android:layout_marginLeft="30sp" android:layout_marginRight="30sp" android:textSize="18sp" android:text="@string/LiveTabBlurb" /> </LinearLayout> <Button android:id="@+id/button_streamstartstop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10sp" android:layout_marginLeft="20sp" android:layout_marginRight="20sp" android:textSize="28sp" android:textStyle="italic" android:textColor="#FFFFFF" android:text="@string/ListenLiveText" android:onClick="LiveButtonClick" /> <ProgressBar android:layout_gravity="center" android:layout_height="100dp" android:layout_width="wrap_content" android:id="@+id/liveProgress" android:paddingTop="30dp" android:indeterminateBehavior="cycle" android:indeterminate="true" android:visibility="gone"> </ProgressBar> <ProgressBar android:id="@+id/liveProgress" style="?android:attr/progressBarStyleLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="20dp" android:visibility="gone" /> </LinearLayout> 

Podcasts.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="160dp" android:layout_height="wrap_content"> <TextView android:id="@+id/currentdateDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="5px" android:text="Today's Date:" /> <TextView android:id="@+id/currentdateDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="5px" android:text="" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="160dp" android:layout_height="wrap_content"> <TextView android:id="@+id/selecteddateDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="5px" android:text="Selected Date:" /> <TextView android:id="@+id/selecteddateDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="5px" android:text="None Selected" /> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Spinner android:id="@+id/spinnerdate" android:layout_width="160dp" android:layout_height="wrap_content" android:prompt="@string/date_prompt" /> <Spinner android:id="@+id/spinnerhour" android:layout_width="160dp" android:layout_height="wrap_content" android:prompt="@string/hour_prompt" /> </LinearLayout> <Button android:id="@+id/button_pod_play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_marginRight="20sp" android:paddingTop="5sp" android:layout_marginTop="10sp" android:textSize="28sp" android:textColor="#FFFFFF" android:textStyle="italic" android:text="@string/PlayPodText" android:onClick="PodButtonClick"/> <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dp" style="?android:attr/progressBarStyleHorizontal" android:layout_margin="10dp" android:id="@+id/seekBar" android:max="100" android:progress="0" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/CurrentTimeText" android:layout_width="60dp" android:layout_height="wrap_content" android:paddingTop="5px" android:textSize="18px" android:text="0:00" /> <ProgressBar android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/podProgress" android:paddingTop="10px" android:indeterminateBehavior="cycle" android:indeterminate="true" android:visibility="gone"> </ProgressBar> <TextView android:id="@+id/TotalTimeText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5px" android:gravity="right" android:textSize="18px" android:text="0:00" /> </LinearLayout> </LinearLayout> 

Un poco más de fondo. Esta aplicación era en realidad 100% de trabajo, pero yo estaba usando un feo Android 2.0 días de interfaz con pestañas y quería actualizar con una interfaz de Holo agradable con adaptador de vista de página. No haber hecho nunca eso antes de que lo probé y estoy recibiendo estos problemas.

ACTUALIZACIÓN: Tengo algunas cosas funcionando (un poco). Al depurar mis dos hiladores ya no son nulos, por lo menos de acuerdo con el depurador. Sin embargo, la aplicación sigue fallando cuando se accede a cualquier objeto de diseño por la actividad. Si intento fijar cualquiera de las textviews o de los hilanderos parece que se bloquean, o apenas no pone al día el contenido.

Código fragmentos:

 public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { //Fragment fragment = new DummySectionFragment(); Fragment fragment; Bundle args = new Bundle(); switch(i){ case 0: fragment = new fragLive(); args.putInt(fragLive.ARG_SECTION_NUMBER, i + 1); break; case 1: fragment = new fragPod(); args.putInt(fragPod.ARG_SECTION_NUMBER, i + 1); break; default: fragment = new fragLive(); args.putInt(fragLive.ARG_SECTION_NUMBER, i + 1); break; } fragment.setArguments(args); return fragment; } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(); case 1: return getString(R.string.title_section2).toUpperCase(); } return null; } } /** * Two classes for the two layout fragments */ public class fragLive extends Fragment { public fragLive() { } public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER); Bundle args = getArguments(); int test = args.getInt(ARG_SECTION_NUMBER); inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); fragviewLive = inflater.inflate(R.layout.live, null); return fragviewLive; } } public class fragPod extends Fragment { public fragPod() { } public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER); Bundle args = getArguments(); int test = args.getInt(ARG_SECTION_NUMBER); inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); fragviewPod = inflater.inflate(R.layout.podcasts, null); return fragviewPod; } } 

Primero compruebe si tiene R.id.spinnerdate en su diseño main.xml.
Si es el caso, pruebe lo siguiente:

Dado que está utilizando una FragmentActivity , infla su diseño primero.

  private View myView; ..... myView = getLayoutInflater().inflate(R.layout.main, null); 

Y cambie la referencia de dataspinner a:

 Spinner datespinner = (Spinner) myView.findViewById(R.id.spinnerdate); 

Necesitas llamar a findViewById() en la vista de tu fragmento:

 Spinner datespinner = (Spinner) fragmentThatContainsSpinner.getView().findViewById(R.id.spinnerdate); 

La cuestión es con la contextualización, que es un poco un problema peculiar. La cuestión del contexto debe ser aplastada y los objetos puestos a disposición sin ningún contexto o cuestiones de alcance. Pero antes de esperar una actualización, podemos usar la solución que se muestra a continuación.

Es decir, cuando se establece un ratón en el oyente de clic en un botón -en el interior- un fragmento …

Ejemplo (null) Fragment Code:

 MyButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View MyClickViewer) { //here the spinner is null? Spinner MySpinner = null; //it is so result is null... MyClickViewer.findViewById(R.id.a_spinner); } } 
  • El MyClickViewer vive dentro del -contexto- de Click View.
  • El MySpinner vive dentro del -contexto- de la Vista Raíz.

Ejemplo (no nulo) Spinner Recuperación:

 //not null anymore... Spinner MySpinner = (Spinner)MyClickViewer.getRootView().findViewById(R.id.a_spinner); 

El método getRootView () le permite crear archivos 'fragment.xml' sin tener que redefinir sus objetos en ningún archivo 'main.xml'. Además de eso, usted puede obtener cualquier valor de la Spinner ya que ahora se refiere y no nulo más.

Ejemplo (no nulo) Spinner Posición:

 MySpinner.getSelectedItemPosition() 
  • "Java.lang.NullPointerException" al intentar instalar Android SDK
  • Esperaba un mapa mientras se deserializaba, pero obtuvo una clase java.lang.Long
  • NullPointerException cuando el acceso a la matriz en la clase
  • Cómo rectificar NullPointerException en v4.DrawerLayout?
  • Android cómo setOnClickListener para el botón de fragmento
  • Error al entregar el resultado ResultInfo
  • Excepción de puntero nulo de Android en la respuesta de Retrofit
  • Intentar invocar método virtual 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler ()' en una referencia de objeto nulo
  • El botón puede producir una excepción de puntero nulo (Android Studio)
  • NullPointerException en onRestoreInstanceState
  • "Algo mal aquí, no esperaba que el paquete se reanude" error en android Logcat
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.