Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';在DDMS中找不到数据库_Android - Fatal编程技术网

Android Can';在DDMS中找不到数据库

Android Can';在DDMS中找不到数据库,android,Android,但是,我将继续学习本教程, 当我尝试时,在DDMS中找不到我的数据库。 为什么会这样 我不确定这是否是代码问题,但是,没有eror日志。 这是我的密码 AndroidDBHelper.java public class AndroidOpenDbHelper extends SQLiteOpenHelper { // Database attributes public static final String DB_NAME = "myDB";

但是,我将继续学习本教程, 当我尝试时,在DDMS中找不到我的数据库。 为什么会这样

我不确定这是否是代码问题,但是,没有eror日志。 这是我的密码

AndroidDBHelper.java

public class AndroidOpenDbHelper extends  SQLiteOpenHelper {

    // Database attributes
            public static final String DB_NAME = "myDB";
            public static final int DB_VERSION = 2;
            // Table attributes
            public static final String TABLE_NAME_LOG = "fuelLog";
            public static final String KEY_ROWID = "_id";
            public static final String KEY_DATE = "date";
            public static final String KEY_PRICE = "fuelprice";
            public static final String KEY_FUEL = "fuelpump";
            public static final String KEY_COST = "tcost";
            public static final String KEY_ODM = "odometer";
            public static final String KEY_CON = "fcon";

            public AndroidOpenDbHelper(Context context) {
                super(context, DB_NAME, null, DB_VERSION);
            }
            // Called when the database is created for the first time. 
            //This is where the creation of tables and the initial population of the tables should happen.


            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                // We need to check whether table that we are going to create is already exists.
                //Because this method get executed every time we created an object of this class. 
                //"create table if not exists TABLE_NAME ( BaseColumns._ID integer primary key autoincrement, FIRST_COLUMN_NAME text not null, SECOND_COLUMN_NAME integer not null);"
                String SQL_createTable = "create table if not exists " + TABLE_NAME_LOG + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
                                                                        + KEY_DATE + " text not null, "
                                                                        + KEY_PRICE + " text not null, "
                                                                        + KEY_FUEL + " text not null, "
                                                                        + KEY_COST + " text not null, "
                                                                        + KEY_ODM + " text not null, "
                                                                        + KEY_CON + " text not null);";
                // Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
                db.execSQL(SQL_createTable);

            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if(oldVersion == 1 && newVersion == 2){
                        // Upgrade the database
                    }       
                }
            }
fuelogpojo.java

public class fuelLogPojo {
     private String date;
     private String price;
     private String pump;
     private String cost;
     private String odometer;
     private String fcon;


     public String getdate() {
             return date;
     }
     public void setdate(String date) {
             this.date = date;
     }

     public String getprice() {
             return price;
     }
     public void setprice(String price) {
             this.price = price;
     }

     public String getpump() {
             return pump;
     }
     public void setpump(String pump) {
             this.pump = pump;
     }

     public String getcost() {
         return cost;
 }
 public void setcost(String cost) {
         this.cost = cost;
 }

 public String getodometer() {
     return odometer;
}
public void setodometer(String odometer) {
     this.odometer = odometer;
}

public String getfcon() {
  return fcon;
}
public void setfcon(String fcon) {
  this.fcon = fcon;
}
}
mainactivity.java

public class MainActivity extends Activity {

    // TableLayout tablelayout_Log = null;
    Button saveButton = null;
    Button cancelButton = null;
   // Button searchButton = null;
    static EditText dateEdit; 
    EditText priceEdit;
    EditText pumpEdit;
    TextView costView;
    EditText odometerEdit;
    TextView fconView;
     TextWatcher textWatcher;
     String priceEditStr ="",pumpEditStr="";
     String  odmEditStr = "";
     String lastOdm = "";

double result;
double resultCon;
private int mYear;
private int mMonth;
private int mDay;

static final int DATE_DIALOG_ID = 0;


private ArrayList fuelLogArrayList;

    public boolean isNumeric(String str)
    {
        return str.matches("-?\\d+(\\.\\d+)?"); 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        costView = (TextView)findViewById(R.id.tcost);
        dateEdit = (EditText)findViewById(R.id.date);
        priceEdit = (EditText)findViewById(R.id.fuelprice);
        pumpEdit = (EditText)findViewById(R.id.fuelpump);
        odometerEdit = (EditText)findViewById(R.id.odometer);
        fconView = (TextView)findViewById(R.id.fcon);
        fuelLogArrayList = new ArrayList();

       // DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
       // lastOdm = dbAdaptor.getLastOdometer();
//Check that your database is enable to fetch the value or not? 
//Toast.makeText(getApplicationContext()," "+lastOdm,Toast.LENGTH_LONG).show();

        dateEdit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });


           priceEdit.addTextChangedListener(new TextWatcher() {

               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it
                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
                        priceEditStr = priceEdit.getText().toString().trim();
                   if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
                        pumpEditStr = pumpEdit.getText().toString().trim();

                  if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                  {
                    result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);              
                    costView.setText(" "+result);
                  }

               }
           });

           pumpEdit.addTextChangedListener(new TextWatcher() {

               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it
                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
                        priceEditStr = priceEdit.getText().toString().trim();
                   if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                        pumpEditStr = pumpEdit.getText().toString().trim();


                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                      {
                        result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);              
                        costView.setText(" "+result);
                      }

               }
           });







           odometerEdit.addTextChangedListener(new TextWatcher() {
               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }



               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it

                   if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                       odmEditStr = odometerEdit.getText().toString().trim();


                  if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase(null) && !lastOdm.trim().equalsIgnoreCase(" "))
                     {

                       resultCon = Double.parseDouble(odmEditStr) / Double.parseDouble(pumpEditStr);              
                       fconView.setText(" "+resultCon);
                     }

               }
           });

    }

    public void onClick(View v) {
        if(v.getId() == R.id.cancelBTN){
            finish();
        }else if(v.getId() == R.id.saveBTN){
            // Get the values provided by the user via the UI


             String date = dateEdit.getText().toString();
             String price = priceEdit.getText().toString();
             String pump = pumpEdit.getText().toString();
             String tcost = costView.getText().toString();
             String odometer = odometerEdit.getText().toString();
             String fcon = fconView.getText().toString();


            // Pass above values to the setter methods in POJO class
             fuelLogPojo fuelLogPojoObj = new fuelLogPojo();
             fuelLogPojoObj.setdate(date);
             fuelLogPojoObj.setprice(price);
             fuelLogPojoObj.setpump(pump);
             fuelLogPojoObj.setcost(tcost);
             fuelLogPojoObj.setodometer(odometer);
             fuelLogPojoObj.setfcon(fcon);
            // Add an undergraduate with his all details to a ArrayList
             fuelLogArrayList.add(fuelLogPojoObj);

            // Inserting undergraduate details to the database is doing in a separate method
            insertLog(fuelLogPojoObj);

            // Release from the existing UI and go back to the previous UI
            finish();
        }
    }

    private void insertLog(fuelLogPojo fuelLogPojoObj) {


        // TODO Auto-generated method stub
        // First we have to open our DbHelper class by creating a new object of that
        AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);

        // Then we need to get a writable SQLite database, because we are going to insert some values
        // SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
        SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();

        // ContentValues class is used to store a set of values that the ContentResolver can process.
        ContentValues contentValues = new ContentValues();

        // Get values from the POJO class and passing them to the ContentValues class
        contentValues.put(AndroidOpenDbHelper.KEY_DATE, fuelLogPojoObj.getdate());
        contentValues.put(AndroidOpenDbHelper.KEY_PRICE, fuelLogPojoObj.getprice());
        contentValues.put(AndroidOpenDbHelper.KEY_FUEL, fuelLogPojoObj.getpump());
        contentValues.put(AndroidOpenDbHelper.KEY_COST, fuelLogPojoObj.getcost());
        contentValues.put(AndroidOpenDbHelper.KEY_ODM, fuelLogPojoObj.getodometer());
        contentValues.put(AndroidOpenDbHelper.KEY_CON, fuelLogPojoObj.getfcon());

        // Now we can insert the data in to relevant table
        // I am going pass the id value, which is going to change because of our insert method, to a long variable to show in Toast
        long affectedColumnId = sqliteDatabase.insert(AndroidOpenDbHelper.TABLE_NAME_LOG, null, contentValues);

        // It is a good practice to close the database connections after you have done with it
        sqliteDatabase.close();

        // I am not going to do the retrieve part in this post. So this is just a notification for satisfaction ;-)
        Toast.makeText(this, "Values inserted column ID is :" + affectedColumnId, Toast.LENGTH_SHORT).show();        





    }

    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker

    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        dateEdit.setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        // set selected date into datepicker also
}}}

首先使用
findViewById
(//此处为您的ID)初始化您的
按钮

其次,为您的
按钮实施
OnClickListener
,以便将内容保存到
数据库中

查看数据库的
文件资源管理器

1)
Windows-->ShowView-->文件资源管理器

2)
data-->data-->您的包名---.databases


3)
如果创建,您将看到您的数据库

首先使用
findViewById
初始化您的
按钮
(//此处为您的ID)

其次,为您的
按钮实施
OnClickListener
,以便将内容保存到
数据库中

查看数据库的
文件资源管理器

1)
Windows-->ShowView-->文件资源管理器

2)
data-->data-->您的包名---.databases


3)
如果已创建,您将看到您的数据库

通过以下步骤从Eclipse中检出您的数据库:

  • 窗口->打开透视图->DDMS
  • 打开文件资源管理器窗口
  • 浏览(数据/数据/应用程序包名称/数据库)**目录
  • 里面有您提供的名为(myDB)的数据库
同时初始化单击侦听器的按钮

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
     saveButton = (Button) findViewById (R.id.yourID );
   cancelButton = (Button) findViewById (R.id.you ID );

   saveButton.setOnClickListener(this);
   cancelButton.setOnClickListener(this);
 }

通过以下步骤从Eclipse中检出数据库:

  • 窗口->打开透视图->DDMS
  • 打开文件资源管理器窗口
  • 浏览(数据/数据/应用程序包名称/数据库)**目录
  • 里面有您提供的名为(myDB)的数据库
同时初始化单击侦听器的按钮

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
     saveButton = (Button) findViewById (R.id.yourID );
   cancelButton = (Button) findViewById (R.id.you ID );

   saveButton.setOnClickListener(this);
   cancelButton.setOnClickListener(this);
 }

您没有初始化按钮,只是在活动中声明了它们

Button saveButton = null;
Button cancelButton = null;
稍后,您需要在
onCreate()
方法中初始化它们,如下所示

saveButton = (Button) findViewById ( your ID );
cancelButton = (Button) findViewById ( you ID );
然后您需要添加OnClickListener

saveButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);

您没有初始化按钮,只是在活动中声明了它们

Button saveButton = null;
Button cancelButton = null;
稍后,您需要在
onCreate()
方法中初始化它们,如下所示

saveButton = (Button) findViewById ( your ID );
cancelButton = (Button) findViewById ( you ID );
然后您需要添加OnClickListener

saveButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);


转到文件资源管理器窗口>显示视图>文件资源管理器>数据>包>数据库名称以在emulator中查看您的数据库

转到文件资源管理器窗口>显示视图>文件资源管理器>数据>包>数据库名称以在emulator中查看您的数据库

您是在设备还是在emulator中测试它?我正在emulator中测试它,您在哪里检查数据库?您必须在活动中创建一个AndroidPendbHelper的实例,然后只创建数据库。好的,您是否从设备中选择了相应的仿真器?在DDMS中选择您的设备,然后查看/data/data/your.app.name/databases是否在设备或仿真器中测试此功能?我正在仿真器中测试它,您在哪里检查数据库?您必须在活动中创建一个AndroidPendbHelper的实例,然后只创建数据库。好的,您是否从设备中选择了相应的模拟器?在DDMS中选择您的设备,然后查看/data/data/your.app.name/databases如前所述,我已经完成了!但是找不到这意味着您的数据库未创建。您是否创建了一个
AndroidPendbHelper
类的实例?@GrIsHu,我印象深刻:D@Chloe由于您已经在button click中编写了代码,并且还没有为click listener注册任何按钮,所以在数据库中插入的代码无法工作。查看我的最新答案,如前所述,我已经做到了!但是找不到这意味着您的数据库未创建。您是否创建了一个
AndroidPendbHelper
类的实例?@GrIsHu,我印象深刻:D@Chloe由于您已经在button click中编写了代码,并且还没有为click listener注册任何按钮,所以在数据库中插入的代码无法工作。查看我的更新answer@Chloe顺便问一下,你听了我的回答了吗?:)如果你还没有找到,请告诉我it@Chloe顺便问一下,你听了我的回答了吗?:)如果你还没找到,请告诉我