免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3994 | 回复: 1
打印 上一主题 下一主题

JAVA显示空指针,请问哪里出问题了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-02 22:42 |只看该作者 |倒序浏览
本帖最后由 Wheat_southwest 于 2014-06-03 07:35 编辑

public class dbhelper extends SQLiteOpenHelper{
    private static final String DB_NAME = "school_activity.db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME1 = "lecture";
    private static final String TABLE_NAME2 = "stu";
    private static final String TABLE_NAME3 = "com";
    private static final String CREATE_INFO1 = "create table if not exists lecture("
            + "_id integer primary key autoincrement,name varchar(20),"
            + "time varchar(20),place varchar(20),hostname varchar(20),"
            +"more varchar(50),contact varchar(20)";
    private static final String CREATE_INFO2 = "create table if not exists stu("
            + "_id integer primary key autoincrement,name varchar(20),"
            + "time varchar(20),place varchar(20),hostname varchar(20),"
            +"more varchar(50),contact varchar(20)";
    private static final String CREATE_INFO3 = "create table if not exists com("
            + "_id integer primary key autoincrement,name varchar(20),"
            + "time varchar(20),place varchar(20),hostname varchar(20),"
            +"more varchar(50),contact varchar(20)";
    private SQLiteDatabase db;
     dbhelper(Context context){
      super(context,DB_NAME,null,DB_VERSION);
}
@Override
    public void onCreate(SQLiteDatabase db){
     this.db=db;
     db.execSQL(CREATE_INFO1);
     Log.i("TABLE1","created successfully!");
     db.execSQL(CREATE_INFO2);
     Log.i("TABLE2","created successfully!");
     db.execSQL(CREATE_INFO3);
     Log.i("TABLE3","created successfully!");
}
    public void insert(SQLiteDatabase MDB, String tableName) {
       ContentValues values=new ContentValues();
        values.put("name","信管");
        values.put("time","2014.6.1");
        values.put("place","204");
        values.put("hostname","名字");
        values.put("more","关于安卓编程");
        values.put("contact","名字  手机");
        MDB.insert(tableName,null,values);
    }
    public Cursor query(String tableName) {
        db = getWritableDatabase();
        Cursor c = db.query(tableName, null, null, null, null, null, null);
        return c;
    }

    // Return cursor by SQL string
    public Cursor rawQuery(String sql, String[] args) {
        db = getWritableDatabase();
        Cursor c = db.rawQuery(sql, args);
        return c;
    }
    public void execSQL(String sql) {
        db = getWritableDatabase();
        db.execSQL(sql);
    }
    public void del(int id,String tableName) {
        if (db == null)
            db = getWritableDatabase();
        db.delete(tableName, "id=?", new String[] { String.valueOf(id) });
    }
    public void close() {
        if (db != null)
            db.close();
    }
    public void onUpgrade(SQLiteDatabase db,int oldversion,int newversion){

    }

}

















public class activities extends Activity {
    SQLiteDatabase db=null;
    dbhelper dbhelper;
    ArrayList<HashMap<String,Object>> listdata;
    SimpleAdapter listItemAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ecnu_type);
        dbhelper=new dbhelper(activities.this);
        dbhelper.onCreate(db);
        dbhelper.insert(db,"lecture");
        Cursor cursor=dbhelper.rawQuery("select name,hostname from lecture",null);
        listdata=new ArrayList<HashMap<String, Object>>();
     //   cursor.moveToFirst();
        int columnsize=cursor.getColumnCount();
        while(cursor.moveToNext()){

            HashMap<String,Object> map=new HashMap<String, Object>();
            for(int i=0;i<columnsize;i++){
                map.put("name", cursor.getString(1));
                map.put("hostname",cursor.getString(4));

            }
            listdata.add(map);
        }
        ListView list=(ListView)findViewById(R.id.activity_items);
        listItemAdapter=new SimpleAdapter(activities.this,listdata,R.layout.item,new String[]{"name","hostname"},
                new int[]{R.id.name,R.id.hostname});
        list.setAdapter(listItemAdapter);


        }

}

论坛徽章:
0
2 [报告]
发表于 2014-06-02 22:46 |只看该作者
这是运行后的结果


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP