请帮助我得到NullPointerException错误。我是片段的新手,我尝试了没有片段的方法,它可以工作,我不明白为什么在片段上实现它时它不起作用,我已经检查了数据库,数据库没有问题,谢谢
E/AndroidRuntime: FATAL EXCEPTION: main
Process: id.co.bumisentosa.yantek, PID: 26979
java.lang.NullPointerException
at id.co.bumisentosa.yantek.fragment_JTM.Inspection_JTM_Fragment_Awal_Tab.onCreateView(Inspection_JTM_Fragment_Awal_Tab.java:95)
这是
Inspection_JTM_Fragment_Awal_Tab
类代码public class Inspection_JTM_Fragment_Awal_Tab extends Fragment implements
OnItemSelectedListener, View.OnClickListener {
// Spinner element
Spinner spinner_unit;
private Context context;
private DatabaseHandler databaseHandler;
static int categoryID;
private List<ItemsDetails> categoryList;
private CategorySpinnerAdapter adapter;
public Inspection_JTM_Fragment_Awal_Tab() {
// Required empty public constructor
}
public static int getCategory() {
return categoryID;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.inspection_jtm_fragment_tab_awal, container, false);
// Spinner element
spinner_unit = (Spinner) view.findViewById(R.id.spinner_unit_inspeksi_jtm);
categoryList = databaseHandler.getCategories();
adapter = new CategorySpinnerAdapter(getActivity(), categoryList);
spinner_unit.setAdapter(adapter);
spinner_unit.setOnItemSelectedListener(this);
return view;
}
我的适配器
public class CategorySpinnerAdapter extends BaseAdapter {
private List<ItemsDetails> list;
private LayoutInflater inflater;
public CategorySpinnerAdapter(Activity activity, List<ItemsDetails> list) {
this.list = list;
this.inflater = LayoutInflater.from(activity);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup root) {
if(view == null){
view = inflater.inflate(R.layout.spinner_item, root, false);
}
TextView categoryName = (TextView) view.findViewById(R.id.text_category_name);
categoryName.setText(list.get(position).getName());
return view;
}
}
我的数据库处理程序
公共类DatabaseHandler扩展了SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "test";
// Contacts table name
private static final String TABLE_CATEGORY = "category";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
//
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORY_TABLES = " CREATE TABLE " + TABLE_CATEGORY
+ "(" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT " + ")";
db.execSQL(CREATE_CATEGORY_TABLES);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY);
// Create tables again
onCreate(db);
}
public List<ItemsDetails> getCategories() {
List<ItemsDetails> detailsList = new ArrayList<ItemsDetails>();
String selectQuery = "SELECT * FROM " + TABLE_CATEGORY;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
try {
ItemsDetails details = new ItemsDetails();
details.setId(cursor.getInt(0));
details.setName(cursor.getString(1));
detailsList.add(details);
} catch (Exception e) {
e.printStackTrace();
}
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return detailsList;
}
}
最佳答案
以此更改您的CategorySpinnerAdapter
public class CategorySpinnerAdapter extends BaseAdapter {
private List<ItemsDetails> list;
private LayoutInflater inflater;
private Context context;
public CategorySpinnerAdapter(Context context, List<ItemsDetails> list) {
this.context=context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class MyHolder {
TextView categoryName;
}
@Override
public View getView(int position, View view, ViewGroup root) {
if(view == null){
view = inflater.inflate(R.layout.spinner_item, root, false);
}
TextView categoryName = (TextView) view.findViewById(R.id.text_category_name);
categoryName.setText(list.get(position).getName());
return view;
}
}
关于java - fragment 内的微调基本适配器上的NullPointerException错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35460317/