在android中的SQLite Db上执行查询时出现NullPointerException

时间:2016-12-24 13:33:14

标签: android database sqlite android-layout nullpointerexception

现在我在这里得到空指针异常。在,ViewAllData方法myAdapter = new SQLCustomViewAllDataAdapter(SQLViewAllData.this,aList); 这里我在创建构造函数时遇到错误。

SQLViewAllData.java

public class SQLViewAllData extends Activity {

private SQLDbHelper myDbHelper;
private SQLCustomViewAllDataAdapter myAdapter;
private ListView myListView;
private ArrayList<SQLPojo.UserDetails> aList = new ArrayList<>();
//private Context myContext;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_view_data);
    //myContext = this;

    widgetInitialization();
    viewAllData();
}
//Initializing Widgets
public void widgetInitialization() {
    myDbHelper = new SQLDbHelper( this );
    myListView = (ListView)findViewById( R.id.listview_view_data_LV );
}

public ArrayList<SQLPojo.UserDetails> getUserInfo(){
    return myDbHelper.getUserDetails();
}

//view all data stored in DB
public void viewAllData() {
    aList = getUserInfo();
    myAdapter = new SQLCustomViewAllDataAdapter(SQLViewAllData.this,aList);
    myListView.setAdapter(myAdapter);
}

}

2 个答案:

答案 0 :(得分:2)

您需要更改:

myDbHelper = new SQLDbHelper( myContext );

myDbHelper = new SQLDbHelper(this);

同时删除&#39;活动myContext&#39;因为你的类已经扩展了Activity。

答案 1 :(得分:1)

你没有初始化上下文,即myContext