如何在空对象引用上修复Intent.getExtras()'?

时间:2016-06-01 15:41:56

标签: java android sqlite listview onitemclick

我想根据网格项目点击填充列表视图。如果用户单击第一个网格项,我想根据它显示列表视图。我在这里使用了一个预先填充的SQLite数据库。

我尝试的是,在第一个活动中获取项目点击ID并将其传递给DB 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;
}}

DBAccess类:

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);

                }




            });}}

}

主要活动类

{{1}}

3 个答案:

答案 0 :(得分:3)

首先,DBAccess不需要扩展Activity,删除它。更新你的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);

}

答案 1 :(得分:0)

您的DBAccess类不是由意图启动的,因此无意获取。

您应该在ContactView类中获取intent和bundle,然后将该字符串传递给DBAccess类中的getQuotes函数。

答案 2 :(得分:0)

您刚刚将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?
    ...
}