如何修复空对象引用上的 Intent.getExtras()'?
How do I fix Intent.getExtras()' on a null object reference?
我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了预填充的 SQLite 数据库。
我尝试的是首先 activity 获取项目点击 ID 并将其传递给数据库 Class。这是我在调试程序时得到的。
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 Class:
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;
}}
联系人视图 Class:
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);
}
}
主要activityclass
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 class 不是由意图启动的,因此没有意图获取。
您应该在 ContactView class 中获取意图和包,然后将字符串传递给 DBAccess class.
中的 getQuotes 函数
首先,DBAccess 不需要扩展 Activity,删除 it.Update 您的 getQuotes
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);
}
您只是将 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;
}
DBAccess.(构造函数):
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?
...
}
我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了预填充的 SQLite 数据库。
我尝试的是首先 activity 获取项目点击 ID 并将其传递给数据库 Class。这是我在调试程序时得到的。
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 Class:
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;
}}
联系人视图 Class:
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);
}
}
主要activityclass
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 class 不是由意图启动的,因此没有意图获取。
您应该在 ContactView class 中获取意图和包,然后将字符串传递给 DBAccess class.
中的 getQuotes 函数首先,DBAccess 不需要扩展 Activity,删除 it.Update 您的 getQuotes
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);
}
您只是将 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;
}
DBAccess.(构造函数):
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?
...
}