我的主页是一个有 6 页的 ViewPager(第一个设置,其他 5 个是 listview 并使用这个 cursoradapter 来获取数据)
在设置中有一个按钮可以打开另一个活动,用户在那里更改一些设置,然后该活动将以finish()(或super.onBackpressed())结束,但每次我调用finish()时都会出现NullPointerException
我究竟做错了什么?
这是我的光标适配器
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;
}
}
这是堆栈跟踪
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)