首页  >  问答  >  正文

android的怎么在一个activity里查询另外一个activity的数据库?

//第一个
case R.id.selectId:

        mmanager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
        Intent intent=new Intent(this, AllSchool.class);
        PendingIntent pending=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification=new Notification.Builder(this)
                .setAutoCancel(true)
                .setTicker("下拉查看")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("学校")
                .setContentText("详细信息")
                .setContentIntent(pending)
                .build();
        mmanager.notify(1,notification);
        break;

//第二个
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.manager);
                    
    listViewId=(ListView)findViewById(R.id.listViewId);
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactList);
    listViewId.setAdapter(adapter);
    readManager();
    
}

public void readManager()
{
    SQLiteDatabase db2=msh.getWritableDatabase();
    Cursor cursor=db2.query("school", null, null, null, null, null, null);
    
    if(cursor.moveToFirst())
    {
        int id=cursor.getInt(cursor.getColumnIndex("id"));
        int phone=cursor.getInt(cursor.getColumnIndex("phone"));
        String name=cursor.getString(cursor.getColumnIndex("name"));
        String address=cursor.getString(cursor.getColumnIndex("address"));
        //Log.d("MainActivity","id is"+id);
        //Log.d("MainActivity","phone is"+phone);
        //Log.d("MainActivity","name is"+name);
        //Log.d("MainActivity","address is"+address);
        
        contactList.add(id+"\n"+phone+"\n"+name+"\n"+address);
        
        
    }

错误提示 空指针异常

曾经蜡笔没有小新曾经蜡笔没有小新2649 天前794

全部回复(1)我来回复

  • 三叔

    三叔2017-06-19 09:09:44

    首先确保你的数据库中有'school'这个表,然后确定这个表有'id''phone''name''address'的字段,接着确定表中有数据,代码中可以在获取到cursor后判断是否为null,打个Log看一下。

    回复
    0
  • 取消回复