NPE when trying to change background color of listview row.on fragment creation

时间:2015-05-24 22:04:33

标签: android android-listview nullpointerexception

I am in the process of learning Android, but i am getting in trouble with something.

I'm trying to change background color of a listView row on fragment creation depending on some data in my database but the thing is i'm just getting Null Pointer exception at every try.

I already implemented the background color change, but this is only working into the onItemClick method.

What i have tried to change the background color when de fragment is created is the following on the onResume(), onViewCreated() methods:

myListView.getChildAt(0).setBackgroundColor(Color.CYAN);

But as i said before i am only getting NPE.

Thank you for your help!

This is where i pupulate my listView:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_lista, container, false);

    DbManager dbm = new DbManager(getActivity());


    alumnosLista = dbm.AlumnosMap();
    arrayAlumnos = new ArrayList<String>();

    for (Map.Entry<Long,Alumno> entry : alumnosLista.entrySet()) {
        arrayAlumnos.add(entry.getValue().getCodigo()+" | "+ entry.getValue().getAp_pat()+" "+entry.getValue().getAp_mat()+", "+entry.getValue().getNombres());
    }

    lista = (ListView) rootView.findViewById(R.id.ListaAlumnos);

    ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1 , arrayAlumnos);
    lista.setAdapter(adaptador);



    lista.setOnItemClickListener(this);
    lista.setOnItemLongClickListener(this);

    return rootView;
}

This is the logcat output:

05-24 16:41:30.513    3901-3901/freeravens.student_stats E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: freeravens.student_stats, PID: 3901
java.lang.NullPointerException
        at freeravens.student_stats.ListaFragment.onViewCreated(ListaFragment.java:159)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5070)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案