我想根据网格项单击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了一个预填充的SQLite数据库。
我尝试的是,在第一个活动中获取项目单击ID并将其传递给数据库类。这就是我在调试程序时得到的。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.sra.hellosrilanka.DBAccess.getQuotes(DBAccess.java:51)
at com.example.sra.hellosrilanka.ContactView.onCreate(ContactView.java:29)
DBAccess 类:
public class DBAccess extends Activity {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
String passedVar=null;
public DBAccess(Context context) {
this.openHelper =new HelloDatabase(context);
}
public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context);
}
return instance;
}
public void open() {
this.database = openHelper.getWritableDatabase();
}
public void close() {
if (database != null) {
this.database.close();
}
}
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Integer value;
Bundle extras = getIntent().getExtras();
String a = extras.getString("ID_EXTRA");
if(extras != null)
{
Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id = ""+ a +""" , null);
/* Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);*/
/*Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);*/
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
cursor.close();
}
}
/* if (passedVar != null) {
}*/
return list;
}}
联系视图类:
public class ContactView extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
this.listView = (ListView) findViewById(R.id.listView);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes();
databaseAccess.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
this.listView.setAdapter(adapter);
}
}
主要活动类
public class MainActivity extends AppCompatActivity {
GridView grid;
Toolbar toolbar;
private ListView listView;
String [] result;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView) findViewById(R.id.grid);
toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
grid.setAdapter(new CustomAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
/*Intent intent=new Intent(MainActivity.this,ContactView.class);
intent.putExtra("catid", id);
startActivity(intent);*/
Intent i=new Intent(MainActivity.this ,ContactView.class);
i.putExtra("ID_EXTRA",String.valueOf(id));
startActivity(i);
}
});}}
首先,DBAccess不需要扩展活动,请将其删除。更新您的获取报价
public List<String> getQuotes(String id) {
List<String> list = new ArrayList<>();
Integer value;
if(id != null)
{
Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id = ""+ id +""" , null);
/* Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);*/
/*Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);*/
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
cursor.close();
}
}
/* if (passedVar != null) {
}*/
return list;
}
并从ContactView中的意图中获取价值。
public class ContactView extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
this.listView = (ListView) findViewById(R.id.listView);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
databaseAccess.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
this.listView.setAdapter(adapter);
}
您的 DBAccess 类不是由意图启动的,因此没有意图获取。
您应该在 ContactView 类中获取意图和捆绑包,然后将字符串传递给 DBAccess 类中的 getQuotes 函数。
您刚刚将 ContactViewActivity 的实例传递给 DBAccess 的构造函数,然后没有保留对它的引用。
ContactViewActivity.onCreate:
DBAccess databaseAccess = DBAccess.getInstance(this);
DBAccess.getInstance:
public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context); // ok let's take a look into the constructor
}
return instance;
}
数据库。(构造函数):
public DBAccess(Context context) {
this.openHelper =new HelloDatabase(context);
// you will lose a reference to this context when returning
}
DBAccess.getQuotes:
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Integer value;
Bundle extras = getIntent().getExtras(); // now, get intent from what?
...
}