设置Visibility Gone时出现NullPointerException

时间:2015-06-16 14:35:54

标签: java android nullpointerexception

我想在片段中隐藏TextView,但当我使用textview.setVisibility(View.GONE);时,我得到NullPointerException,问题出在哪里?我已经初始化了textview,所以我不明白为什么会出现这个错误。

这是我的代码的一部分:

 @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-- 我解决了我的问题,我试图在另一个视图中访问textview,我创建了一个适配器并且它可以工作:)

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);  

    }

}

0 个答案:

没有答案