关于android中路径获取的问题

依旧00 2014-05-28 10:55:59
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
这个该怎么解决呢?求各位前辈指点一下。
...全文
318 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
braddoris 2014-05-29
  • 打赏
  • 举报
回复
将类AddressDao 的构造函数修改为 pubic AddressDao (Context context) { this.context = context; } 然后在调用的时候获取 context.getFilesDir().getPath()获取路径。
技术小黑屋 2014-05-29
  • 打赏
  • 举报
回复
我建议你先学习一下java基础,如果不学好的话,可能学习android也会比较吃力.你可以看一下Thinking In Java中文版.
引用 2 楼 zhong1113 的回复:
[quote=引用 1 楼 BossDarcy 的回复:] getFilesDir是对象方法不是类方法,所以调用必须使用Context实例,如果在android组件中可以使用getApplicationContext()来获取context对象.
还是不太明白怎么用,下面贴上具体代码
public class AddressDao {
	public static final String path = "/data/data/com.zhong.mobilesafe/files/address.db";
	/**
	 * 获取手机号码的归属地
	 * 
	 * @param number
	 *            传入的手机号码
	 * @return 归属地
	 */
	public static String getAddress(String number) {
		String address = number;
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
				SQLiteDatabase.OPEN_READONLY);
		if (number.matches("^1[3458]\\d{9}$")) {
			Cursor cursor = db
					.rawQuery(
							"select location  from data2 where id=(select outkey  from data1 where id=?)",
							new String[] { number.substring(0, 7) });

			if (cursor.moveToNext()) {
				address = cursor.getString(0);
			}
			cursor.close();
		}
		db.close();
		return address;
[/quote]
依旧00 2014-05-28
  • 打赏
  • 举报
回复
引用 1 楼 BossDarcy 的回复:
getFilesDir是对象方法不是类方法,所以调用必须使用Context实例,如果在android组件中可以使用getApplicationContext()来获取context对象.
还是不太明白怎么用,下面贴上具体代码
public class AddressDao {
	public static final String path = "/data/data/com.zhong.mobilesafe/files/address.db";
	/**
	 * 获取手机号码的归属地
	 * 
	 * @param number
	 *            传入的手机号码
	 * @return 归属地
	 */
	public static String getAddress(String number) {
		String address = number;
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
				SQLiteDatabase.OPEN_READONLY);
		if (number.matches("^1[3458]\\d{9}$")) {
			Cursor cursor = db
					.rawQuery(
							"select location  from data2 where id=(select outkey  from data1 where id=?)",
							new String[] { number.substring(0, 7) });

			if (cursor.moveToNext()) {
				address = cursor.getString(0);
			}
			cursor.close();
		}
		db.close();
		return address;
技术小黑屋 2014-05-28
  • 打赏
  • 举报
回复
getFilesDir是对象方法不是类方法,所以调用必须使用Context实例,如果在android组件中可以使用getApplicationContext()来获取context对象.

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧