关于android中路径获取的有关问题
关于android中路径获取的问题
public static final String path ="/data/data/com.zhong.mobilesafe/files/address.db";
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
在path哪里提示Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
改成这样: public static final String path =Context.getFilesDir().getPath()+"/com.zhong.mobilesafe/files/address.db";
又提示:Cannot make a static reference to the non-static method getFilesDir() from the type Context
这个该怎么解决呢?求各位前辈指点一下。
------解决方案--------------------
getFilesDir是对象方法不是类方法,所以调用必须使用Context实例,如果在android组件中可以使用getApplicationContext()来获取context对象.
------解决方案--------------------
我建议你先学习一下java基础,如果不学好的话,可能学习android也会比较吃力.你可以看一下Thinking In Java中文版.
------解决方案--------------------
将类AddressDao 的构造函数修改为
pubic AddressDao (Context context)
{
this.context = context;
}
然后在调用的时候获取
context.getFilesDir().getPath()获取路径。
public static final String path ="/data/data/com.zhong.mobilesafe/files/address.db";
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
在path哪里提示Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
改成这样: public static final String path =Context.getFilesDir().getPath()+"/com.zhong.mobilesafe/files/address.db";
又提示:Cannot make a static reference to the non-static method getFilesDir() from the type Context
这个该怎么解决呢?求各位前辈指点一下。
------解决方案--------------------
getFilesDir是对象方法不是类方法,所以调用必须使用Context实例,如果在android组件中可以使用getApplicationContext()来获取context对象.
------解决方案--------------------
我建议你先学习一下java基础,如果不学好的话,可能学习android也会比较吃力.你可以看一下Thinking In Java中文版.
------解决方案--------------------
将类AddressDao 的构造函数修改为
pubic AddressDao (Context context)
{
this.context = context;
}
然后在调用的时候获取
context.getFilesDir().getPath()获取路径。