我无法在Marshmallow中运行我的应用程序。我怎么解决这个问题?

时间:2016-12-28 05:49:27

标签: android sqlite

我可以将我的应用程序运行到 Android 5.1.1 。但是无法进入主页直到Android 6(数据库已创建并且数据可以在这里插入),应用程序在这里停止工作。

Android 6以上数据库无法创建。任何人都可以提供解决方案......

DBHelper类

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DatabaseName="/mnt/sdcard/Hmeauto1_DB.db";
    private static final int DatabaseVersion=1;
    SQLiteDatabase db;
    public String c=null;





        public static final String Query = "CREATE TABLE " + UserConstruct.newUserinfo.TableName + " ( " + UserConstruct.newUserinfo.Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserConstruct.newUserinfo.UserName + " TEXT, " + UserConstruct.newUserinfo.Password + " TEXT," + UserConstruct.newUserinfo.FName + " TEXT, " + UserConstruct.newUserinfo.LName + " TEXT," + UserConstruct.newUserinfo.ACode + " TEXT," + UserConstruct.newUserinfo.AEmail + " TEXT, " + UserConstruct.newUserinfo.User_Type + " TEXT);";

        public static final String Query1 = "CREATE TABLE  " + UserConstruct.add_home.TableName + " ( " + UserConstruct.add_home.Home_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserConstruct.add_home.Home_Name + " TEXT, " + UserConstruct.add_home.Home_Number + " INTEGER, " + UserConstruct.add_home.UserName + " TEXT);";

        public static final String Query2 = "CREATE TABLE  " + UserConstruct.add_Floors.TableName + " ( " + UserConstruct.add_Floors.Flr_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserConstruct.add_Floors.Home_Id + " INTEGER, " + UserConstruct.add_Floors.Flr_Name + " TEXT, " + UserConstruct.add_Floors.Flr_Number + " INTEGER, " + UserConstruct.add_Floors.UserName + " TEXT ;";

        public static final String Query3 = "CREATE TABLE  " + UserConstruct.add_Rooms.TableName + " ( " + UserConstruct.add_Rooms.Room_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserConstruct.add_Rooms.Flr_Id + " INTEGER, " + UserConstruct.add_Rooms.Home_Id + " INTEGER, " + UserConstruct.add_Rooms.Room_Name + " TEXT, " + UserConstruct.add_Rooms.Room_Number + " INTEGER, " + UserConstruct.add_Rooms.UserName + " TEXT ;";

        // public static final String Floor="CREATE TABLE "+UserConstruct.Floors.TableName+" ( "+UserConstruct.Floors.Flr_Id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+UserConstruct.Floors.Flr_Name+" TEXT);";
        public static final String Query4 = "CREATE TABLE  " + UserConstruct.image_list.TableName + " ( " + UserConstruct.image_list.Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserConstruct.image_list.Image_Name_fxd + " TEXT, " + UserConstruct.image_list.Image_Name + " TEXT, " + UserConstruct.image_list.Device_ID + " TEXT, " + UserConstruct.image_list.Home_Id + " INTEGER, " + UserConstruct.image_list.Flr_Id + " INTEGER, " + UserConstruct.image_list.Room_Id + " INTEGER, " + UserConstruct.image_list.UserName + " TEXT);";


        public DatabaseHelper(Context context)
        {
            super(context, DatabaseName, null, DatabaseVersion);
            Log.e("Database operation", "created or opened";
            //  Log.d("path:", String.valueOf(context.getDatabasePath(DatabaseName)));

        }


        @Override
        public void onCreate (SQLiteDatabase db)
        {
            try {
                db.execSQL(Query);
                Log.e("Created", "User Table Created";
                db.execSQL(Query1);
                Log.e("Created", "Add Home Table Created";
                db.execSQL(Query2);
                Log.e("Created", "Add Floor Table Created";
                db.execSQL(Query3);
                Log.e("Created", "Add Room Table Created";
                db.execSQL(Query4);
                Log.e("Created", "Add Room Table Created";
            }catch (RuntimeException e){e.printStackTrace();}


        }

        @Override
        public void onUpgrade (SQLiteDatabase db,int oldVersion, int newVersion)
        {
            try {
                String query = "DROP TABLE IF EXIST" + UserConstruct.newUserinfo.TableName;
                db.execSQL(query);
                String query1 = "DROP TABLE IF EXIST" + UserConstruct.add_Rooms.TableName;
                db.execSQL(query1);
                String query2 = "DROP TABLE IF EXIST" + UserConstruct.add_Floors.TableName;
                db.execSQL(query2);
                String query3 = "DROP TABLE IF EXIST" + UserConstruct.add_home.TableName;
                db.execSQL(query3);
                String query4 = "DROP TABLE IF EXIST" + UserConstruct.image_list.TableName;
                db.execSQL(query4);
            }catch(RuntimeException e){e.printStackTrace();}
            //  this.db=db;

        }

    public void insert(String Fname, String Lname, String Aemail, String Acode, String userName, String Password, String userType, SQLiteDatabase db) {
        ContentValues contentvalue = new ContentValues();
        contentvalue.put(UserConstruct.newUserinfo.FName, Fname);
        contentvalue.put(UserConstruct.newUserinfo.LName, Lname);
        contentvalue.put(UserConstruct.newUserinfo.AEmail, Aemail);
        contentvalue.put(UserConstruct.newUserinfo.ACode, Acode);
        contentvalue.put(UserConstruct.newUserinfo.UserName, userName);
        contentvalue.put(UserConstruct.newUserinfo.Password, Password);
        contentvalue.put(UserConstruct.newUserinfo.User_Type, userType);

        db.insert(UserConstruct.newUserinfo.TableName, null, contentvalue);
        Log.e("Insertion", "One row inserted";
        db.close();


    }

注册数据

View.OnClickListener adduserOnClickListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
        fname=(EditText)findViewById(R.id.editTextFname);
        sname=(EditText)findViewById(R.id.editTextsname);
        email=(EditText)findViewById(R.id.editTextEmail);
        code=(EditText)findViewById(R.id.editTextcode);
        Uname=(EditText)findViewById(R.id.editTextuser);
        pwrd=(EditText)findViewById(R.id.editTextpass);
        //utype=(Spinner)findViewById(R.id.spinnerutype);

        String fname1 = fname.getText().toString();
        String lname1 = sname.getText().toString();
        String email1 = email.getText().toString();
        String code1 = code.getText().toString();
        String uname1 = Uname.getText().toString();
        String pswd1 = pwrd.getText().toString();
        String utype1 = "admin";


        if (fname1.equals("") {
            Toast.makeText(getBaseContext(), "Enter First Name", Toast.LENGTH_LONG).show();

        } else if (lname1.equals("") {
            Toast.makeText(getBaseContext(), "Enter Second Name", Toast.LENGTH_LONG).show();
        } else if (email1.equals("") {
            Toast.makeText(getBaseContext(), "Enter Email Id ", Toast.LENGTH_LONG).show();
        } else if (code1.equals("") {
            Toast.makeText(getBaseContext(), "Enter Authentication Code ", Toast.LENGTH_LONG).show();
        } else if (uname1.equals("") {
            Toast.makeText(getBaseContext(), "Enter User Name", Toast.LENGTH_LONG).show();
        } else if (pswd1.equals("") {
            Toast.makeText(getBaseContext(), "Enter Password ", Toast.LENGTH_LONG).show();
        }else {
            try {
                sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
                Cursor allrows19 = sqlitedatabase.rawQuery("SELECT * FROM " + UserConstruct.newUserinfo.TableName + " WHERE " + UserConstruct.newUserinfo.UserName + "=='" + uname1 + "'", null);
                if (allrows19.getCount() <= 0) {
                   // Toast.makeText(getBaseContext(), "Not exist", Toast.LENGTH_LONG).show();
                    exist = false;

                } else if (allrows19.getCount() < 1) {
                    exist = false;
                   // Toast.makeText(getBaseContext(), "Not exist", Toast.LENGTH_LONG).show();
                }

                allrows19.close();
                dbhelper.close();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "",
                        Toast.LENGTH_LONG);
            }


            if (exist) {

                Toast.makeText(getBaseContext(), "This User Name not available", Toast.LENGTH_LONG).show();

            }
            else
            {
                dbhelper = new DatabaseHelper(context);
                sqlitedatabase = dbhelper.getWritableDatabase();
                dbhelper.insert(fname1, lname1, email1, code1, uname1, pswd1, utype1, sqlitedatabase);
                Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
                dbhelper.close();
                Registration.this.finish();
                Intent intent=new Intent(Registration.this,LoginActivity.class);
                startActivity(intent);


            }
        }

   // }



        }catch (NullPointerException e){e.printStackTrace();}


    }
};

2 个答案:

答案 0 :(得分:0)

将数据库路径更改为您的应用专用路径,否则您需要运行时change sd card files权限才能在SD卡上写入数据

PackageManager m = getPackageManager(); 
String s = getPackageName(); 
try { 
       PackageInfo p = m.getPackageInfo(s, 0); 
       s = p.applicationInfo.dataDir; 
} catch (PackageManager.NameNotFoundException e) {
       Log.w("yourtag", "Error Package name not found ", e); 
}

因此s变量是您的私有应用程序路径
如果上述解决方案无法正常工作,请提供一些logcat日志

答案 1 :(得分:0)

您应该使用以下方法检查用户是否已授予外部存储权限:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    Log.v(TAG,"Permission is granted");
    return true;
}

如果没有,您需要让用户授予您的应用权限:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

当然这些仅适用于棉花糖设备,因此您需要检查您的应用是否在Marshmallow上运行:

 if (Build.VERSION.SDK_INT >= 23) {
      //do your check here
 }

还要确保您的活动实施OnRequestPermissionResult

整个权限如下:

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

权限结果回调:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}