mycode错了我不知道为什么无法打开数据库错误代码14

时间:2017-11-21 18:43:00

标签: android database sqlite spinner

我将从数据库中显示获取spinner thats项目,我不知道为什么无法找到数据库

这是我的databaseopenhelper

public class DtabaseOpenHelper_loghat extends SQLiteOpenHelper {

    private static String DB_NAME = "my_db_vokabel";
    private static String DB_PATH = "";
    private static int DB_VERSION = 1;
    private SQLiteDatabase mydatabase;
    private Context mycontext;

    public DtabaseOpenHelper_loghat(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

        if (Build.VERSION.SDK_INT >= 15) {
            DB_PATH = context.getApplicationInfo().dataDir + "/database/";
        } else {
            DB_PATH = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/data/";
        }
        this.mycontext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void checkandcopy_database() {
        boolean dbExist = check_database();
        if (dbExist) {
            Log.d("TAG", "database already exist");

        } else {

            this.getReadableDatabase();
        }
        try {
            copydatabase();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("TAG", "database already exist");
        }

    }

    public boolean check_database() {
        SQLiteDatabase checkDB = null;
        try {
            String mypath = DB_PATH + DB_NAME;

            checkDB = SQLiteDatabase.openDatabase(mypath, null, OPEN_READWRITE);
        } catch (SQLException e) {
        }
        if (checkDB != null) {
            checkDB.close();

        }
        return
                checkDB != null ? true : false;

    }

    private void copydatabase() throws IOException {

        InputStream myinputstream = mycontext.getAssets().open(DB_NAME);
        String outfilename = DB_PATH + DB_NAME;
        OutputStream myoutput = new FileOutputStream(outfilename);
        byte[] buffer = new byte[1024];
        int lenght;
        while ((lenght = myinputstream.read(buffer)) > 0) {
            myoutput.write(buffer, 0, lenght);

        }
        myoutput.flush();
        myoutput.close();
        myinputstream.close();
    }

    public void openDatabe() {
        String mypath = DB_PATH + DB_NAME;

        mydatabase = SQLiteDatabase.openDatabase(mypath, null, OPEN_READWRITE);

    }

    public synchronized void close() {

        if (mydatabase != null) {
            mydatabase.close();

        }
        super.close();
    }

    public Cursor querydata(String query) {
        return mydatabase.rawQuery(query, null);


    }

    // baraye khandane fallen az database baraye spinner //

    public List<String> getAllLabels() {
        List<String> items = new ArrayList<>();

        // Select All Query

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from fallen", null);


        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                items.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning lables
        return items;
    }


}

这是我的帖子

public class Post {


    private Drawable fav_icone;
    private String loghat;
    private String artikel;
    private String bedeutung;
    private String mesal;



    public Drawable getFav_icone() {
        return fav_icone;
    }

    public void setFav_icone(Drawable fav_icone) {
        this.fav_icone = fav_icone;
    }

    public String getLoghat() {
        return loghat;
    }

    public void setLoghat(String loghat) {
        this.loghat = loghat;
    }

    public String getArtikel() {
        return artikel;
    }

    public void setArtikel(String artikel) {
        this.artikel = artikel;
    }

    public String getBedeutung() {
        return bedeutung;
    }

    public void setBedeutung(String bedeutung) {
        this.bedeutung = bedeutung;
    }

    public String getMesal() {
        return mesal;
    }

    public void setMesal(String mesal) {
        this.mesal = mesal;
    }
}

这是我在这里的活动,我将从tabel(下降)获取数据并在spinner中显示

public class Add_loghat_activity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    TextView textview_1;
    RadioButton radio_der, radio_die, radio_das;
    MaterialBetterSpinner spinn_fallen;
    EditText edit_nloghat, edit_bedeut, edit_mesal;
    Button btn_back;
    Button btn_sabt;
    DtabaseOpenHelper_loghat my_db;
    ArrayAdapter myAdapter;
    SQLiteDatabase db;
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_loghat_activity);

        radio_der = findViewById(R.id.artkel_der);
        radio_die = findViewById(R.id.artkel_die);
        radio_das = findViewById(R.id.artkel_das);
        edit_nloghat = findViewById(R.id.edit_text_loghat);
        edit_bedeut = findViewById(R.id.edit_text_bedeutung);
        edit_mesal = findViewById(R.id.edit_text_mesal);
        spinn_fallen = findViewById(R.id.spinner_fallen);

       loadSpinnerData();

        // animation for textview in addloghat//
        textview_1 = findViewById(R.id.text_view1);
        textview_1.setText("کلمه جدید را وارد کنید");
        textview_1.setAnimation(AnimationUtils.loadAnimation(Add_loghat_activity.this, android.R.anim.slide_in_left));


        //values in spenner for fallen//


           //disabel kardane btn sabt//

        btn_sabt = findViewById(R.id.btn_sabt);
        btn_sabt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Okay man guuut", Toast.LENGTH_LONG).show();
            }
        });

        //btn back //
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent int_back = new Intent(Add_loghat_activity.this, My_activity.class);
                startActivity(int_back);

            }
        });

        btn_sabt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });


    }

    private void loadSpinnerData() {
        // database handler
        my_db = new DtabaseOpenHelper_loghat(getApplicationContext());
        my_db.openDatabe();

        // Spinner Drop down elements
        List<String> items = my_db.getAllLabels();

        // Creating adapter for spinner
        myAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, items);

        // Drop down layout style - list view with radio button
        myAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinn_fallen.setAdapter(myAdapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }


    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

这是我的适配器

public class LoghatsAdapter extends RecyclerView.Adapter<LoghatsAdapter.LoghatViewHolder> {

    private Context context;
    private List<Post> postha = Collections.emptyList();
    private Activity activity;
    private OnTabListener ontabmylistener;

    public LoghatsAdapter(Activity activity, List<Post> postha) {
        this.activity = activity;
        this.postha = postha;

    }

    public LoghatsAdapter() {
        this.context = context;
        this.postha = postha;

    }

    @Override
    public LoghatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View viewitem = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_loghat, parent, false);
        return new LoghatViewHolder(viewitem);
    }

    @Override
    public void onBindViewHolder(LoghatViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.loghat.setText(postha.get(position).getLoghat());
        holder.artikel.setText(postha.get(position).getArtikel());
        holder.bedeutung.setText(postha.get(position).getBedeutung());
        holder.mesal.setText(postha.get(position).getMesal());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ontabmylistener != null) {
                    ontabmylistener.OnTabView(position);

                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return postha.size();
    }

    public void setOntabmylistener(OnTabListener ontabmylistener) {
        this.ontabmylistener = ontabmylistener;

    }

    class LoghatViewHolder extends RecyclerView.ViewHolder {
        private TextView loghat;
        private TextView artikel;
        private TextView bedeutung;
        private TextView mesal;
        private ImageButton img_favorite;


        public LoghatViewHolder(View itemView) {
            super(itemView);
            loghat = (TextView) itemView.findViewById(R.id.textview_loghat);
            artikel = (TextView) itemView.findViewById(R.id.textview_artikel);
            bedeutung = (TextView) itemView.findViewById(R.id.textview_bedeutung);
            mesal = (TextView) itemView.findViewById(R.id.textview_mesal);
            img_favorite = (ImageButton) itemView.findViewById(R.id.img_btn_favorite);
        }
    }
}

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.et.vokabelheft_1, PID: 6237
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.et.vokabelheft_1/com.example.et.vokabelheft_1.Activitis.Add_loghat_activity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
   Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
      at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
      at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
      at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
      at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
      at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
      at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
      at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:750)
      at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:699)
      at com.example.et.vokabelheft_1.DtabaseOpenHelper_loghat.openDatabe(DtabaseOpenHelper_loghat.java:109)
      at com.example.et.vokabelheft_1.Activitis.Add_loghat_activity.loadSpinnerData(Add_loghat_activity.java:101)
      at com.example.et.vokabelheft_1.Activitis.Add_loghat_activity.onCreate(Add_loghat_activity.java:51)
      at android.app.Activity.performCreate(Activity.java:7000)
      at android.app.Activity.performCreate(Activity.java:6991)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

0 个答案:

没有答案