Domanda

My main page is an ViewPager with 6 pages (first one settings, other 5 are listview and use this cursoradapter for data)

In settings there is one button which opens another activity, users change some settings there and then that activity will end with finish() (or super.onBackpressed()) but everytime I call finish() an NullPointerException comes up

What am I doing wrong?

This is my CursorAdapter

package com.whd.roosters.adapters;

import com.whd.roosters.R;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class RoosterCursorAdapter extends SimpleCursorAdapter {
    private Cursor cursor;
    private Context context;

    @SuppressWarnings("deprecation") //New constructor available from API 11
    public RoosterCursorAdapter(Context mContext, int layout, Cursor mCursor, String[] from, int[] to) {
        super(mContext, layout, mCursor, from, to);
        cursor = mCursor;
        context = mContext;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RoosterCursorAdapter(Context mContext, int layout, Cursor mCursor, String[] from, int[] to, int flags) {
        super(mContext, layout, mCursor, from, to, flags);
        cursor = mCursor;
        context = mContext;
    }

    public int getImage(int uur) {
        if (uur == 1) return R.drawable.eerste;
        if (uur == 2) return R.drawable.tweede;
        if (uur == 3) return R.drawable.derde;
        if (uur == 4) return R.drawable.vierde;
        if (uur == 5) return R.drawable.vijfde;
        if (uur == 6) return R.drawable.zesde;
        if (uur == 7) return R.drawable.zevende;
        if (uur == 8) return R.drawable.achtste;
        return 1;
    }

    public View getView(int pos, View row, ViewGroup root) {
        cursor.moveToPosition(pos);

        String uur = cursor.getString(cursor.getColumnIndex("uur"));
        String les = cursor.getString(cursor.getColumnIndex("les"));
        String lok = cursor.getString(cursor.getColumnIndex("lok"));
        String doc = cursor.getString(cursor.getColumnIndex("doc"));

        ImageView UUR = null;
        TextView  LES = null;
        TextView  LOK = null;
        TextView  DOC = null;

        if (lok.contains("Vrij")) {
            if (row == null) row = ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.listview_item_rooster_vrij, null);
            UUR = ( ImageView ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_VRIJ_IMAGEVIEW_UUR );
            LOK = ( TextView  ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_VRIJ_TEXTVIEW_VRIJ );
        } else {
            if (row == null) row = ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.listview_item_rooster, null);
            UUR = ( ImageView ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_IMAGEVIEW_UUR   );
            LES = ( TextView  ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_TEXTVIEW_LES    );
            LOK = ( TextView  ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_TEXTVIEW_LOKAAL );
            DOC = ( TextView  ) row.findViewById( R.id.LISTVIEW_ITEM_ROOSTER_TEXTVIEW_DOCENT );
        }

        UUR.setAdjustViewBounds(true);
        if ((context.getResources().getDisplayMetrics().heightPixels / 10) > 10) {
            UUR.setMaxHeight(context.getResources().getDisplayMetrics().heightPixels / 10);
        } else {
            UUR.setMaxHeight(100);
        }
        UUR.setMinimumHeight(50);
        UUR.setImageResource(getImage(Integer.parseInt(uur)));

        LOK.setText(lok);
        if (LES != null) LES.setText("Les: " + les);
        if (DOC != null) DOC.setText("Docent: " + doc);

        return row;
    }
}

This is the stacktrace

04-07 16:08:39.430: E/AndroidRuntime(4562): FATAL EXCEPTION: main
04-07 16:08:39.430: E/AndroidRuntime(4562): java.lang.NullPointerException
04-07 16:08:39.430: E/AndroidRuntime(4562):     at com.whd.roosters.adapters.RoosterCursorAdapter.getView(RoosterCursorAdapter.java:72)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.AbsListView.obtainView(AbsListView.java:1597)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.ListView.fillDown(ListView.java:695)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.ListView.fillGap(ListView.java:666)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4215)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.widget.AbsListView$TwFlingRunnable.run(AbsListView.java:3431)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.os.Handler.handleCallback(Handler.java:587)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.os.Looper.loop(Looper.java:130)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at android.app.ActivityThread.main(ActivityThread.java:3691)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at java.lang.reflect.Method.invoke(Method.java:507)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-07 16:08:39.430: E/AndroidRuntime(4562):     at dalvik.system.NativeStart.main(Native Method)
È stato utile?

Soluzione

your problem is on line 72:

UUR.setAdjustViewBounds(true);

my guess would be that you have some problem with your "if" statement, and while you think you got the right layout, I guess you are getting the other one.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top