2

I want to hide a TextView in a Fragment, but when I use textview.setVisibility(View.GONE);, I get NullPointerException, where is the problem? I have already initialized the textview, so I don't understand why I get this error.

This is a part of my code:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.lista_activity, container, false);
        ListView scarsdaleweek;
        TextView spuntino;
        TextView merenda;
        TextView recordspuntino;
        TextView recordmerenda;
        Button btnscarsdale;
        scarsdaleweek = (ListView) rootView.findViewById(R.id.listaweek);
            d= new Database(getActivity());
            String scelta=d.checkScelta();
            final Cursor c = d.week(scelta);
            CursorLoader(c);
            if(scelta.equals("scarsdale")||scelta.equals("atkins")){



            String from[] = {
                    Codice.DATI_ID,
                    Codice.COLAZIONE,
                    Codice.PRANZO,
                    Codice.CENA,                
                    };

            int to[] = {
                    R.id.record_id,
                    R.id.record_colazione,
                    R.id.record_pranzo,
                    R.id.record_cena,
                    };
            @SuppressWarnings("deprecation")
            SimpleCursorAdapter sca = new SimpleCursorAdapter(rootView.getContext(),
                    R.layout.singolo_elemento_week,
                    c, from, to);
            scarsdaleweek = (ListView) rootView.findViewById(R.id.listaweek);
            scarsdaleweek.setAdapter(sca);
            spuntino =(TextView)rootView.findViewById(R.id.text_spuntino);
            merenda =(TextView)rootView.findViewById(R.id.text_merenda);
            recordspuntino =(TextView)rootView.findViewById(R.id.record_spuntino);
            recordmerenda =(TextView)rootView.findViewById(R.id.record_merenda);

            spuntino.setVisibility(View.GONE);
            merenda.setVisibility(View.GONE);
            recordspuntino.setVisibility(View.GONE);
            recordmerenda.setVisibility(View.GONE);
            }

Logcat:

06-16 16:45:20.372: E/AndroidRuntime(23428): FATAL EXCEPTION: main
06-16 16:45:20.372: E/AndroidRuntime(23428): Process: com.lp.lemiediete, PID: 23428
06-16 16:45:20.372: E/AndroidRuntime(23428): java.lang.NullPointerException
06-16 16:45:20.372: E/AndroidRuntime(23428):    at com.lp.lemiediete.Lista.onCreateView(Lista.java:69)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.os.Handler.handleCallback(Handler.java:733)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.os.Looper.loop(Looper.java:136)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at android.app.ActivityThread.main(ActivityThread.java:5021)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at java.lang.reflect.Method.invoke(Method.java:515)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-16 16:45:20.372: E/AndroidRuntime(23428):    at dalvik.system.NativeStart.main(Native Method)

EDIT-- I solved my problem, I was trying to access a textview in another view, I created an adapter and it works :)

public class CustomFontExampleAdapterLista extends SimpleCursorAdapter 
{

 Database d;
    @SuppressWarnings("deprecation")
    public CustomFontExampleAdapterLista(final Context context, final int layout, final Cursor c, final String[] from,
                final int[] to) {
            super(context, layout, c, from, to);

    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        super.bindView(view, context, cursor);

        final TextView spuntino =(TextView)view.findViewById(R.id.text_spuntino);
        spuntino.setVisibility(View.GONE);  
        final TextView merenda =(TextView)view.findViewById(R.id.text_merenda);
        merenda.setVisibility(View.GONE);  

    }

}
Rivas202
  • 215
  • 2
  • 8
  • 2
    1. Always provide a stack trace with your question. 2. You don't have a variable called "textview". 3. Nullpointerexceptions are extremely well documented on Stackoverflow, do a search before asking something new. – Knossos Jun 16 '15 at 14:37
  • @Knossos 1.I have edited my question, I add logcat , 2. The name of the variable was an example , 3. I have already searched but I can't find anything for my case – Rivas202 Jun 16 '15 at 14:48
  • This is already marked as duplicate. Please follow the suggested link to resolve your issue. – Knossos Jun 16 '15 at 14:52
  • as you didn't stated which line has number 69 in your `Lista` class, my guess is `scarsdaleweek` is not found in your view, and when you are try invoke line `scarsdaleweek.setAdapter(sca);` it throws exception – user902383 Jun 16 '15 at 15:09
  • @user902383 no, the 69 line is `spuntino.setVisibility(View.GONE);` – Rivas202 Jun 17 '15 at 06:38
  • it is still same principle, it tried find field in your view, and it couldn't find it. my gess is you were looking for it in wrong view – user902383 Jun 17 '15 at 08:57

0 Answers0