مرسی از شما دوست عزیز
فقط مشکل من اینجاست که من برای ایجاد دیتابیس به مشکل برخوردم و طبق آموزش های استاد دیتابیس رو در کلاس G نوشتن
و من هم مثل دوستانی که در زیر تایپیک شما در مورد اینکه خود کلاس G از Application اکستنزد میشه مشکل برام پیش اومده
شما در پاسخ به یکی از دوستانی که این سوال رو پرسیدن جواب دادین که " از کانستراکتوری که کانتکست میگیره استفاده کنید " میشه اینو توضیح بدین
من الان دوتا کلاسی که گقتین رو ایجاد کردم ولی توو ادامش به مشکل برخوردم
بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ
مشکل در ایجاد دیتابیس در API 23 ؟؟؟
مدیریت گفتگو
آخرین تغییر: 9 سال پیش
با سلام خدمت دوستان عزیز
مشکل من در ایجاد دیتابیس در API 23 ( اندروید 6 ) هست که در ادامه کدی که نوشتم و logcat رو براتون میذارم .
لازمه بدونید که این کدی که نوشتم در اندروید نسخه 5 هیچ مشکلی نداره و درست کار میکنه
کد دیتابیس در کلاس G :
package activities;
import java.io.File;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.widget.Toast;
import fragments.BankFragment;
public class G extends Application {
public static Context context;
public static LayoutInflater inflater;
public static final Handler HANDLER = new Handler();
public static Activity currentActivity;
public static StructApplication selectedApplication;
public static StructReports selectedReports;
public static StructTestResume selectedTestResume;
public static SQLiteDatabase database;
public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_APP = DIR_SDCARD + "/weighbridge-test0012";
public static final String DIR_TEMP = DIR_APP + "/temp";
public static final String DIR_APK = DIR_APP + "/apk";
public static final String DIR_FINAL = DIR_APP + "/final";
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
new File(DIR_APP).mkdirs();
new File(DIR_APK).mkdirs();
new File(DIR_TEMP).mkdirs();
new File(DIR_FINAL).mkdirs();
manageDatabase();
}
private void manageDatabase() {
database = SQLiteDatabase.openOrCreateDatabase(DIR_APP + "/database10.sqlite", null);
G.database.execSQL("CREATE TABLE IF NOT EXISTS tests (" +
"test_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , " +
"Weighbridge_Name TEXT, " +
"Test_Type TEXT, " +
"Test_Date TEXT, " +
"Test_Result TEXT" +
")");
database.execSQL("CREATE TABLE IF NOT EXISTS app (" +
"app_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , " +
"Weighbridge_Name TEXT, " +
"City TEXT, " +
"Town TEXT, " +
"Address TEXT, " +
"Manager_Name TEXT, " +
"Manager_Mobile TEXT, " +
"Tel TEXT, " +
"Fax TEXT, " +
"Date TEXT," +
"Weighbridge_Company TEXT, " +
"Indicator TEXT, " +
"Loadcell_Name TEXT, " +
"Loadcell_Model TEXT, " +
"Max INTEGER," +
"Loadcell_Num INTEGER," +
"Tozin_Test_Date TEXT," +
"Tozin_Test_Result TEXT" +
")");
}
}
و اما logcat :
03-02 04:01:28.839 25301-25305/? E/art: Failed sending reply to debugger: Broken pipe
03-02 04:01:29.187 25301-25301/weighbridgetest.app.rad.max.com.weighbridgetest E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
03-02 04:01:29.187 25301-25301/weighbridgetest.app.rad.max.com.weighbridgetest E/SQLiteLog: (14) os_unix.c:31278: (2) open(/storage/emulated/0/weighbridge-test0012/database10.sqlite) -
03-02 04:01:29.188 25301-25301/weighbridgetest.app.rad.max.com.weighbridgetest E/SQLiteDatabase: Failed to open database '/storage/emulated/0/weighbridge-test0012/database10.sqlite'.
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:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
at activities.G.manageDatabase(G.java:51)
at activities.G.onCreate(G.java:48)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:370)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-02 04:01:29.188 25301-25301/weighbridgetest.app.rad.max.com.weighbridgetest E/AndroidRuntime: FATAL EXCEPTION: main
Process: weighbridgetest.app.rad.max.com.weighbridgetest, PID: 25301
java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
at activities.G.manageDatabase(G.java:51)
at activities.G.onCreate(G.java:48)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:370)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
a
پاسخها 2
مشاهده پاسخ صحیح
پاسخ صحیح
توی اندروید 6 به بعد باید بعضی از پرمیشن ها رو به صورت ران تایم بگیرید و فقط تعریفشون توی منیفست کافی نیست: لینک
نظرات
متوجه نشدم. لینک اون تاپیکی که گفتید رو بزارید و یکم بیشتر توضیح بدید.
منظورم همین لینکی که خودتون گذاشتید رو میگم
ببینید من الان دوتا کلاسی که گفتین (BaseActivity و PermissionHandler ) رو تو پروژم ایجاد کردم و حالا نمیدونم که اون کانستراکتوری که در آخر نوشتین رو باید کجا دقیقا بذارم
من کد دیتابیس رو در کلاس G نوشتم ( کد رو بالا قرار دادم )
آیا داخل همین کلاس کانستراکتور رو بنویسم ( اگه اینجا بخوام بنویسم جواب نمیده ، چون خود کلاس G از Application اکستند شده )
و اگه داخل Main Activity خودم بخوام بنویسم چجوری باید باشه ؟!
والا من که کانستراکتوری ندیدم، اون تاپیک رو من ننوشتم بهتره توی اون تاپیک سوالتون رو بزارید و لینک این تاپیک رو هم بدید که خود آقای حاج حسینی جواب بدن.
آها من فکر کردم شما این مشکل رو حل کردید ...
ای کاش فقط شما به من میگفتید که دقیقا باید به کدی که بالا گذاشتم چی باید اضافه کنم
بازم اشکالی نداره دم شما گرم
آخه مشکل اینجاست که من از این کدها اصلا استفاده نکردم چون پرمیشن هایی که می خواستم نیاز به گرفتن به صورت ران تایم نبود. یه بار تا وسطاشو نوشتم بعد فهمیدم که پرمیشن ها رو نمی خواید ران تایم بگیرم و دیگه ادامه ندادم، وگرنه حتما خودم جوابتون رو میدادم.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
ویرایش
پیام
نظرات