将项目加载到微调器时出现空指针异常
Posted
技术标签:
【中文标题】将项目加载到微调器时出现空指针异常【英文标题】:Null pointer exception when loading items to the spinner 【发布时间】:2016-01-22 12:22:08 【问题描述】:这是我的活动类,我有微调器。我需要将数据库值加载到微调器,所以使用 ArrayList 和 ArrayAdapter,我做到了。
但是当我运行我的应用程序时,它给出了 Null 指针异常。
public class addexpense extends ActionBarActivity
DBhelper helper;
SQLiteDatabase db;
Spinner spinner;
@Override
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.addexpenses);
ArrayList<category> mArrayList = helper.getCategories();
Spinner sp =(Spinner)findViewById(R.id.spinner);
ArrayAdapter adapter =new ArrayAdapter(this,R.layout.spinner_row,mArrayList);
sp.setAdapter(adapter);
这是我在 DBHelper 数据库类中的方法
public class DBhelper extends SQLiteOpenHelper
static final String DATABASE = "wedding9.db";
static final int VERSION = 9;
static final String TABLE1 = "Category";
static final String TABLE2 = "Budget";
static final String C_ID = "_id";
static final String Name = "name";
static final String B_ID = "_id";
static final String Description = "description";
static final String Amount = "amount";
public DBhelper(Context context)
super(context, DATABASE, null, VERSION);
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE " + TABLE1+ "(" +C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +Name+ " text unique not null)");
db.execSQL("CREATE TABLE " + TABLE2+ "(" +B_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +Description+ " text,"
+Amount+ " text, FOREIGN KEY ("+Description+") REFERENCES "+TABLE1+"("+Name+"));");
// db.execSQL("CREATE TABLE " + TABLE2+ "(" +B_ID
// + " INTEGER PRIMARY KEY AUTOINCREMENT," +Amount+ " text )");
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("drop table " + TABLE1);
onCreate(db);
public ArrayList<category> getCategories()
ArrayList<category> arrayList = new ArrayList<category>();
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
// c.moveToFirst();
while (c.moveToNext())
category cat = new category(c.getInt(0),c.getString(1));
arrayList.add(cat);
return arrayList;
public boolean checkIdExist(String name)
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
// c.moveToFirst();
while (c.moveToNext())
if(c.getString(1).equals(name))
return false;
return true;
这是分类类
public class category
private int id;
private String name;
public category(int id, String name)
this.id = id;
this.name = name;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
下面一个是异常,我在运行应用程序时遇到的
10-23 08:29:21.482 3075-3075/com.example.username.weddingplanning E/androidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException:无法启动活动 ComponentInfocom.example.username.weddingplanning/com.example.username.weddingplanning.addexpense: java.lang.NullPointerException 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2304) 在 android.app.ActivityThread.access$700(ActivityThread.java:152) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:176) 在 android.app.ActivityThread.main(ActivityThread.java:5299) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 在 dalvik.system.NativeStart.main(本机方法) 引起:java.lang.NullPointerException 在 com.example.username.weddingplanning.addexpense.onCreate(addexpense.java:43) 在 android.app.Activity.performCreate(Activity.java:5326) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2304) 在 android.app.ActivityThread.access$700(ActivityThread.java:152) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:176) 在 android.app.ActivityThread.main(ActivityThread.java:5299) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 在 dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
你的按钮在哪里?可以发一下吗? 【参考方案1】:首先创建您的帮助程序实例。
helper 在helper.getCategories();
处为空是问题所在。
【讨论】:
10-23 09:06:59.571 8798-8798/com.example.username.weddingplanning E/ArrayAdapter﹕ You must supply a resource ID for a TextView 10-23 09:06:59.601 8798-8798/com.example.username.weddingplanning E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
` ArrayAdapter 适配器 =new ArrayAdapter(this,R.layout.spinner_row,R.id.tv,mArrayList);` 更改此行后,它正在工作
,但是在微调器com.example.username.weddingplan.category@41a1da80
中的值是以这种格式出现的【参考方案2】:
您必须实现 Array 适配器(扩展到 ArrayAdapter)。并覆盖 getDropDownView。
这是样品。
public class CategorySpinnerAdapter extends ArrayAdapter<Category>
List<Category> list = new ArrayList<>();
private Context context;
public CategorySpinnerAdapter(Context context, int resource, Category[] categories)
super(context, resource, categories);
this.context = context;
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
return getMyView(position, convertView, parent);
@Override
public View getView(int position, View convertView, ViewGroup parent)
return getMyView(position, convertView, parent);
private View getMyView(int position, View convertView, ViewGroup parent)
CSViewHolder viewHolder;
if(convertView == null)
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_spinner_row, parent, false);
viewHolder = new CSViewHolder(convertView);
convertView.setTag(viewHolder);
else
viewHolder = (CSViewHolder)convertView.getTag();
viewHolder.fill(list.get(position));
return convertView;
/**
* Load channel list
* @param cursor
*/
public void setList(Cursor cursor)
list.clear();
Log.i(getClass().getName(), String.valueOf(cursor));
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
// The Cursor is now set to the right position
Category category = new Category();
category.setTitle(CursorUtil.getStringColumnFromCursor(cursor, GoftagramContract.CategoryEntry.COLUMN_TITLE));
category.setId(CursorUtil.getStringColumnFromCursor(cursor, GoftagramContract.CategoryEntry.COLUMN_SERVER_ID));
list.add(category);
@Override
public int getCount()
return list.size();
@Override
public Category getItem(int position)
return list.get(position);
class CSViewHolder
private TextView titleView;
private ImageView thumbnailView;
public CSViewHolder(View itemView)
titleView = (TextView)itemView.findViewById(R.id.category_spinner_row_title);
thumbnailView = (ImageView)itemView.findViewById(R.id.category_spinner_row_image);
public void fill(Category category)
titleView.setText(category.getTitle());
if(!TextUtils.isEmpty(category.getThumbnail()))
Glide.with(context)
.load(category.getThumbnail())
.into(thumbnailView);
else
thumbnailView.setImageResource(R.drawable.ic_discuss);
使用适配器
categorySpinnerAdapter = new CategorySpinnerAdapter(this, R.layout.category_spinner_row, new Category[]);
【讨论】:
以上是关于将项目加载到微调器时出现空指针异常的主要内容,如果未能解决你的问题,请参考以下文章