我想使用来自SQLite数据库的数据加载一个微调器。但我不能这样做,我得到如下错误。
12-11 13:05:39.206 2445-2445/com.ksfr.updatetest E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
12-11 13:05:39.206 2445-2445/com.ksfr.updatetest E/AndroidRuntime: at com.ksfr.updatetest.update_fragment02.loadToSpinner(update_fragment02.java:58)
12-11 13:05:39.206 2445-2445/com.ksfr.updatetest E/AndroidRuntime: at com.ksfr.updatetest.update_fragment02.onCreateView(update_fragment02.java:47)
我在我的数据库中的onCreate方法中添加了一行,因为我得到了NullPointerException。即使我无法解决我的问题。
这是我的MyDBHandler.java类
package com.ksfr.updatetest;
/**
* Created by User on 12/10/2015.
*/
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "schoolDB.db";
private static final String TABLE_SCHOOLS = "schools";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_SCHOOLNAME = "schoolname";
public static final String COLUMN_SCHOOLADDRESS = "schooladdress";
public static final String COLUMN_SCHOOLTYPE = "schooltype";
public static final String COLUMN_LAT = "_lat";
public static final String COLUMN_LNG = "_lng";
public MyDBHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_SCHOOLS_TABLE = "CREATE TABLE " +
TABLE_SCHOOLS + "("+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_SCHOOLNAME+" TEXT,"+COLUMN_SCHOOLADDRESS+
" TEXT,"+COLUMN_SCHOOLTYPE+" TEXT,"+COLUMN_LAT+" DOUBLE,"+COLUMN_LNG+" DOUBLE"+")";
db.execSQL(CREATE_SCHOOLS_TABLE);
//Here one item added to avoid to table getting empty.
String add1= "INSERT INTO schools ('schoolname','schooladdress','schooltype','_lat','_lng') " +
"VALUES ('Diyatalawa Central Collage','Diyatalawa','Primary school','2.564','5.6456')";
db.execSQL(add1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCHOOLS);
onCreate(db);
}
* Getting all schools
* returns list of schools
* */
public List<String> getAllSchools(){
List<String> school_list = new ArrayList<String>();
// Select All Query
String Query = "SELECT "+ COLUMN_SCHOOLNAME +" FROM " + TABLE_SCHOOLS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(Query, null);
//School school = new School();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
school_list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return school_list;
}
}
在我的片段类中如下
public class update_fragment02 extends Fragment implements AdapterView.OnItemSelectedListener {
TextView schoolID,schoolName,address,latitude,longitude;
Spinner spinner_school;
Button button_e_edit,button_e_cancel;
String schoolSelect;
public static update_fragment02 newInstance() {
update_fragment02 fragment = new update_fragment02();
return fragment;
}
public update_fragment02(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.update_tab_fab02, container, false);
schoolID =(TextView)rootView.findViewById(R.id.txtID02E);
schoolName=(TextView)rootView.findViewById(R.id.txtName02E);
address=(TextView)rootView.findViewById(R.id.txtAddress02E);
latitude=(TextView)rootView.findViewById(R.id.txtLat02E);
longitude=(TextView)rootView.findViewById(R.id.txtLng02E);
spinner_school= (Spinner)rootView.findViewById(R.id.spinner);
loadToSpinner();
return rootView;
}
private void loadToSpinner(){
MyDBHandler dbHandler =new MyDBHandler(getActivity(),null,null,1);
List<String> schoolList = dbHandler.getAllSchools();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this.getActivity(),
android.R.layout.simple_spinner_item, schoolList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_school.setAdapter(dataAdapter);
spinner_school.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
schoolSelect= String.valueOf(parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}