رفتن به مطلب
انجمن اندروید ایران | آموزش برنامه نویسی اندروید و موبایل
  • android.png.1fab383bc8500cd93127cebc65b1dcab.png

مشکل در اجرای یک برنامه ساده


پست های پیشنهاد شده

سلام.سال نو به تمام دوستان تبریک عرض می کنم

من تازه برنامه نویسی اندروید شروع کردم یک برنامه ساده نوشتم ولی تو اجرا مشکل داشت

فایل برنامه پیوست کردم ممنون می شم راهنمایی کنید

dbtest2.zip

لینک ارسال
به اشتراک گذاری در سایت های دیگر

برنامه ساده است 

یک buttonکه سرش کلیک می کنی اطلاعاتی از فیلد دیتابیس می خونه و نمایش می ده ...........


فایل دیتا بیس تو فایل  ascert هستش

دیتا  بیس با sqlite طراحی شده ...........


زمانی که می خوام اجرا کنم کلوز می زنه


دوستان کمک

لینک ارسال
به اشتراک گذاری در سایت های دیگر

فورس کلوز اول واسه اینکه تو فایل main1.xml دو تا ویجت با id یکسان یعنی btn دارین آی دی relativelayout درست کنین مشکل حل میشه

اما وقتی رو دکمه بزنین بازم فورس کلوز میشه اونم بخاطر اشتباهی که در نامگذاریا دارین 

اسم دیتابیس شما database پس متغیر name رو چرا database1 قرار دادین ؟

متغیر path هم به این صورت اصلاح کنین 

[shcode=java]

    public final String   path = "data/data/com.example.dbtest2/databases/";

[/shcode]

ضمنا برنامه رو از روی شبیه ساز حذف یا کلیر دیتا کنین و بعد اجرا کنین تا تغییرات اعمال بشه

لینک ارسال
به اشتراک گذاری در سایت های دیگر

ممنون بابت پاسخ شما

مشکل button حل شد......

ولی دیتابیس حل نشد

   public final String   path = "data/data/com.example.dbtest2/dataofstorys/";
   public final String   Name = "dataofstorys";
   public SQLiteDatabase mydb;
   private final Context mycontext;


   public database(Context context) {
       super(context, "dataofstorys", null, 1);
       mycontext = context;
   }


دوست عزیز ممنون مشکل حل شد ......

لینک ارسال
به اشتراک گذاری در سایت های دیگر

از این کلاس برای کپی شدن  دیتابیس خود استفاده کنید:

[shcode=java]

public class DatabaseAssets extends SQLiteOpenHelper {

    String DB_PATH = null;

    private static String DB_NAME = "database.db";

    protected static final String TABLE_COMPANY = "tbl_company";

       private boolean upgradeDatabase = false;

    private SQLiteDatabase myDataBase;

 

    private final Context myContext;

    /**

     * Constructor Takes and keeps a reference of the passed context in order to

     * access to the application assets and resources.

     *

     * @param context

     */

    @SuppressLint("SdCardPath")

    public DatabaseAssets(Context context) {

        super(context, DB_NAME, null, 4);

        this.myContext = context;

        // DB_PATH = "/data/data/" + context.getPackageName() + "/" +

        // "databases/";

        DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";

    }

    /**

     * Creates a empty database on the system and rewrites it with your own

     * database.

     * */

    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {

            // do nothing - database already exist

        } else {

            // By calling this method and empty database will be created into

            // the default system path

            // of your application so we are gonna be able to overwrite that

            // database with our database.

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }

        }

    }

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {

            String myPath = DB_PATH + DB_NAME;

            checkDB = SQLiteDatabase.openDatabase(myPath, null,

                    SQLiteDatabase.OPEN_READONLY

                            | SQLiteDatabase.NO_LOCALIZED_COLLATORS

                            | SQLiteDatabase.CREATE_IF_NECESSARY);

        } catch (SQLiteException e) {

            // database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;

    }

    /**

     * Copies your database from your local assets-folder to the just created

     * empty database in the system folder, from where it can be accessed and

     * handled. This is done by transfering bytestream.

     * */

    private void copyDataBase() throws IOException {

        // Open your local db as the input stream

        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db

        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream

        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile

        byte[] buffer = new byte[1024];

        int length;

        while ((length = myInput.read(buffer)) > 0) {

            myOutput.write(buffer, 0, length);

        }

        // Close the streams

        myOutput.flush();

        myOutput.close();

        myInput.close();

    }

    public void openDataBase() throws SQLException {

        // Open the database

        String myPath = DB_PATH + DB_NAME;

        // SQLiteDatabase.NO_LOCALIZED_COLLATORS

        myDataBase = SQLiteDatabase.openDatabase(myPath, null,

                SQLiteDatabase.OPEN_READONLY

                        | SQLiteDatabase.NO_LOCALIZED_COLLATORS

                        | SQLiteDatabase.CREATE_IF_NECESSARY);

    }

    @Override

    public synchronized void close() {

        if (myDataBase != null)

            myDataBase.close();

        super.close();

    }

    @Override

    public void onCreate(SQLiteDatabase db) {

    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

           upgradeDatabase = true;

    }

    // return cursor

    public Cursor query(String table, String[] columns, String selection,

            String[] selectionArgs, String groupBy, String having,

            String orderBy) {

        return myDataBase.query(TABLE_COMPANY, null, null, null, null, null, null);

    }

}

[/shcode]

فراخوانی کلاس دیتابیس برای کپی شدن دیتابیس در مسیر مورد نظر:

[shcode=java]

      DatabaseHelper myDbHelper;

      SQLiteDatabase myDb = null;

  myDbHelper = new DatabaseHelper(this);

            /*

             * Database must be initialized before it can be used. This will ensure

             * that the database exists and is the current version.

             */

             myDbHelper.initializeDataBase();

             try {

                // A reference to the database can be obtained after initialization.

                myDb = myDbHelper.getWritableDatabase();

                /*

                 * Place code to use database here.

                 */

             } catch (Exception ex) {

                ex.printStackTrace();

             } finally {

                try {

                    myDbHelper.close();

                } catch (Exception ex) {

                    ex.printStackTrace();

                } finally {

                    myDb.close();

                }

            }

[/shcode]

لینک ارسال
به اشتراک گذاری در سایت های دیگر

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...