【问题标题】:NullPointerException in android while fetching data from SQLite database? [duplicate]从SQLite数据库获取数据时Android中的NullPointerException? [复制]
【发布时间】:2015-09-17 06:06:03
【问题描述】:

我正在尝试以字符串数组的形式从 SQLite 数据库中获取数据。

在我的活动中:

public class CreateTagActivity extends Activity  {

    ListView tagList;
    DBFunctions db;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_tag);

        tagList=(ListView)findViewById(R.id.list_of_tags);
        db=new DBFunctions(CreateTagActivity.this);

        String[] tagsLists=db.getAllTag();

        ArrayAdapter adapter=new ArrayAdapter(CreateTagActivity.this,android.R.layout.simple_list_item_1,tagsLists);
        tagList.setAdapter(adapter);
}
}

在我的数据库文件中:

public class DBFunctions extends SQLiteOpenHelper {
private static final String DATABASE_NAME="maindb";
private static final int VERSION=1;
private static final String event_tags="CREATE TABLE "+TABLE_TAGS+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,tag TEXT)";
 private final Context context;

public DBFunctions(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(event_tags);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS "+event_tags);
    onCreate(db);
}
 public long insertTag(String tag)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put("tag",tag);
    long id=db.insert(TABLE_TAGS,null,values);
    return id;
}
public String[] getAllTag()
{
    SQLiteDatabase db=this.getWritableDatabase();
    String[] columns={"tag"};
    Cursor cursor=db.query(TABLE_TAGS,columns,null,null,null,null,null);
    int length=cursor.getCount();
    String[] value=new String[length];
    while(cursor.moveToNext())
    {
        int i=0;
        int index=cursor.getColumnIndex("tag");
        value[i]=cursor.getString(index);
        i++;
    }
    return value;
}
}

然后我在 tagsList 中得到了一个错误 NullPointerException

这是错误日志:

09-17 11:17:03.695    3084-3084/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pack.proj PID: 3084
java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
        at android.widget.ListView.onMeasure(ListView.java:1175)
        at android.view.View.measure(View.java:16540)

请帮助我。 谢谢。

【问题讨论】:

  • 在处理sqlite db时不要使用ArrayAdapter,而是使用[Simple]CursorAdapter
  • getAllTag方法中设置断点并调试。
  • ArrayAdapter(this, android.R.layout.simple_list_item_1, values)
  • 如果你创建一个适配器而不是后适配器
  • ArrayAdapter itemsAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);

标签: java android sqlite nullpointerexception


【解决方案1】:
int i=0;

在每次循环迭代中,您将索引重置为零。数组中的所有其他索引保持为null

NPE 是由提供给ArrayAdapter 的数组中的null 项引起的。

要修复它,请将int i=0 移到while 循环之外。

【讨论】:

  • 它有效。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-05-08
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多