현재 스피너를 이용해서 DB에 잇는 데이터를 리스트뷰에 뿌려주고 있습니다.
헌데 다른 탭을 갔다가 다시 돌아오면 업데이트가 되어야 하기 때문에 onResume에 리쿼리를 호출하고 있습니다.
Adapter 문제인지 탭간 전환시 다른 메서드를 호출해줘야 하는지 잘 모르겟어요.
도움 부탁드립니다.
OnResume(){
Adapter.getCursor().requery(); //이것만 호출하면 널포인터 오류를 뿜어내고 있어요.
}
public class History extends Activity { private static final String TAG = "History"; private DBHelper mHelper; ListView list; Cursor cursor; SimpleCursorAdapter Adapter; ImageView image; ArrayAdapter<CharSequence> adspin; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.history_main); list = (ListView)findViewById(R.id.list); image = new ImageView(this); // (1) image.setImageResource(R.drawable.no_data); mHelper = new DBHelper(this); //cursor = mHelper.call_cur(0); Spinner spin = (Spinner)findViewById(R.id.myspinner); spin.setPrompt("보기모드를 선택하세요.");
adspin = ArrayAdapter.createFromResource(this, R.array.view_select, android.R.layout.simple_spinner_item); adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(adspin);
spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(position==0){ cursor = mHelper.call_cur(0); //모두 startManagingCursor(cursor); Adapter = new SimpleCursorAdapter(History.this, R.layout.cus_list, cursor, new String[] { "_id", "Time", "Location", "Distance", "Temp"}, new int[] { R.id.text1, R.id.text2, R.id.text3,R.id.text4,R.id.text5}); if(cursor.getCount()!=0){ list.setAdapter(Adapter); }else{ //setContentView(image); } }else if(position==1){ cursor = mHelper.call_cur(1); //GPS startManagingCursor(cursor); Adapter = new SimpleCursorAdapter(History.this, R.layout.cus_gps, cursor, new String[] { "_id", "Time", "Location", "Distance"}, new int[] { R.id.text1, R.id.text2, R.id.text3,R.id.text4}); if(cursor.getCount()!=0){ list.setAdapter(Adapter); }else{ //setContentView(image); } }else{ cursor = mHelper.call_cur(2); //Temp startManagingCursor(cursor); Adapter = new SimpleCursorAdapter(History.this, R.layout.cus_temp, cursor, new String[] { "_id", "Time", "Temp"}, new int[] { R.id.text1, R.id.text2, R.id.text5}); if(cursor.getCount()!=0){ list.setAdapter(Adapter); }else{ //setContentView(image); } } } public void onNothingSelected(AdapterView<?> parent) { } }); }
@Override public void onResume(){ super.onResume(); Log.i(TAG, "onResume()"); //Adapter.getCursor().requery(); } }
오류
09-07 14:30:46.547: ERROR/AndroidRuntime(253): FATAL EXCEPTION: main
09-07 14:30:46.547: ERROR/AndroidRuntime(253): java.lang.RuntimeException: Unable to resume activity {exam.Kidnaping/.History}: java.lang.NullPointerException
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:138)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.view.View.performClick(View.java:2408)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.view.View$PerformClick.run(View.java:8816)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.os.Handler.handleCallback(Handler.java:587)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.os.Looper.loop(Looper.java:123)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at dalvik.system.NativeStart.main(Native Method)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): Caused by: java.lang.NullPointerException
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at .History.onResume(History.java:95)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.Activity.performResume(Activity.java:3823)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
09-07 14:30:46.547: ERROR/AndroidRuntime(253): ... 17 more
Spinner는 아이템이 추가되면 첫 아이템이 무조건 셀렉트 되는 성격을 가지고 있긴 하나....
순서가 참 절묘하군요.... ㅡㅡㅋ 오류를 찾아가신 케이스입니다.
spin.setAdapter(adspin); 이 문장을
리스너 setOnItemSelectedListener 뒤에만 놔도 에러 안날거 같은데요...
그리고 가장 확실한 방법은 최초케이스에 대해서 Adapter를 초기화 해주는겁니다.
위에 소스를 보면 Listener에만 의존하지 Adapter의 초기화 부분이 없으니까요...