想做一个字典
按这个方法做的
http://www.cnblogs.com/nokiaguy/archive/2010/07/31/1789218.htmlprivate SQLiteDatabase openDatabase()
    {
        try
        {
            // 获得dictionary.db文件的绝对路径
            String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
            File dir = new File(DATABASE_PATH);
            // 如果/sdcard/dictionary目录中存在,创建这个目录
            if (!dir.exists())
                dir.mkdir();
            // 如果在/sdcard/dictionary目录中不存在
            // dictionary.db文件,则从res\raw目录中复制这个文件到
            // SD卡的目录(/sdcard/dictionary)
            if (!(new File(databaseFilename)).exists())
            {
                // 获得封装dictionary.db文件的InputStream对象
                InputStream is = getResources().openRawResource(R.raw.dictionary);
                FileOutputStream fos = new FileOutputStream(databaseFilename);
                byte[] buffer = new byte[8192];
                int count = 0;
                // 开始复制dictionary.db文件
                while ((count = is.read(buffer)) > 0)
                {
                    fos.write(buffer, 0, count);
                }                fos.close();
                is.close();
            }
            // 打开/sdcard/dictionary目录中的dictionary.db文件
            SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
                    databaseFilename, null);
            return database;
        }
        catch (Exception e)
        {
        }
        return null;
    }
但执行到InputStream is = getResources().openRawResource(R.raw.dictionary);就会直接跳到异常处理
异常为空指针异常
已添加    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>
权限

解决方案 »

  1.   

    是不是raw中的文件大小不能超过1M
      

  2.   

    是不是context的问题?context到底是什么呢?怎么改也改不对啊
      

  3.   

    是getResources()为空,也就是context为空,是context的问题,你要看看你代码的上下文。
      

  4.   

    这个context应该是那个?activity的?
    context具体都有啥作用
      

  5.   

    sorry! I get this message a little late,It's something wrong with the "context",you can connect me with qq871874241.I use English because I don't have a input method in my computer now (in ubuntu) ....
      

  6.   

    楼主急求究竟是用哪个context 我用了activity的也不对
      

  7.   

    在调用这个getResources().openRawResource(R.raw.dictionary);时
    如果你是分装在其他类中就必须用context 上下文路径 context.getResources().openRawResource(R.raw.dictionary);
    这样就ok了
      

  8.   


    在调用这个getResources().openRawResource(R.raw.dictionary);时
    如果你是分装在其他类中就必须用context 上下文路径 context.getResources().openRawResource(R.raw.dictionary);
    这样就ok了