2013-09-03 3 views
0

저는 안드로이드와 SQLite 데이터베이스를 처음 사용합니다. 데이터 폴더 내부의 DDMS에서 데이터베이스 테이블을 만들 수 없습니다.이 코드는 내 코드이며, 데이터베이스에 값을 삽입 할 수없고 데이터베이스 테이블이 생성되지 않습니다.Sqlite 데이터베이스 테이블이 생성되지 않습니까?

public class BookingOpenHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "BookingDB"; 
    public static final String BOOKING_TABLE_NAME = "OnlineBooking"; 
    public static final String NAME= "name"; 
    public static final String QUANTITY = "quantity"; 
    public static final String NOTE = "note"; 
    public static final String NUMBER_OF_PIECES = "noofpieces"; 
    public static final String DATE_AND_TIME = "dateandtime"; 
    public static final String DELIVARY_ADDRESS = "delivaryaddress"; 
    public static final int VERSION = 3; 
    public static final String CONTACT_NO = "contactno";  
    public static final String BOOKING_ID = "bookingid";  
    public static final String B_ID = "_id"; 



    private static final String CREATE_BOOKING_TABLE = "create table onlinebooking(_id integer not null primary key autoincrement,name text null,contactnumber integer null,quantity text null,noofpcs integer null,dateandtime text null,delivaryaddress text null,note text null)"; 


    public BookingOpenHelper(Context context, String name, CursorFactory factory, int version) { 
     super(context, name, factory, version); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db1) { 
     // TODO Auto-generated method stub 
     db1.execSQL(CREATE_BOOKING_TABLE); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     onCreate(db1); 
    } 

} 


Adapter.java 


package com.example.android; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 

public class BookingAdapter { 
    SQLiteDatabase database_ob; 
    BookingOpenHelper openHelper_ob; 
    Context context; 

    public BookingAdapter(Context c) {// constructor 
     context = c; 
    } 


    public BookingAdapter opnToRead() {// method for open and read the database to perform the operations 
     openHelper_ob = new BookingOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION); 
     database_ob = openHelper_ob.getReadableDatabase(); 
     return this; 
    } 

    public BookingAdapter opnToWrite() {// method for open and write the database to perform the operations 
     openHelper_ob = new BookingOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION); 
     database_ob = openHelper_ob.getWritableDatabase(); 
     return this; 
    } 

    public void Close() {// method for closing the database 
     database_ob.close(); 
    } 

    public long insertDetails(String name, String contactnumber,String quantity,String noofpieces,String dateandtime,String delivaryaddress,String note) {// insert method for steps 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(openHelper_ob.NAME, name); 
     contentValues.put(openHelper_ob.CONTACT_NO,contactnumber); 
     contentValues.put(openHelper_ob.QUANTITY, quantity);   
     contentValues.put(openHelper_ob.NUMBER_OF_PIECES,noofpieces); 
     contentValues.put(openHelper_ob.DATE_AND_TIME, dateandtime); 
     contentValues.put(openHelper_ob.DELIVARY_ADDRESS,delivaryaddress); 
     contentValues.put(openHelper_ob.NOTE,note);  
     opnToWrite(); 
     long val = database_ob.insert(openHelper_ob.BOOKING_TABLE_NAME, null, contentValues); 
     Close(); 
     return val; 
    } 

    public Cursor queryForSteps() {// method to display the edit profile name and its related steps. 
     String[] cols = {openHelper_ob.BOOKING_ID, openHelper_ob.NAME, openHelper_ob.CONTACT_NO, openHelper_ob.QUANTITY,openHelper_ob.NUMBER_OF_PIECES,openHelper_ob.DATE_AND_TIME,openHelper_ob.DELIVARY_ADDRESS,openHelper_ob.NOTE }; 
     opnToWrite(); 
     Cursor c = database_ob.query(openHelper_ob.BOOKING_TABLE_NAME, cols,null, null, null, null, null); 
     return c; 
    } 

    public Cursor queryAll() {// method to display the edit profile 
     String[] cols = { openHelper_ob.BOOKING_ID, openHelper_ob.NAME, openHelper_ob.CONTACT_NO, openHelper_ob.QUANTITY,openHelper_ob.NUMBER_OF_PIECES,openHelper_ob.DATE_AND_TIME,openHelper_ob.DELIVARY_ADDRESS,openHelper_ob.NOTE }; 
     opnToWrite(); 
     Cursor c = database_ob.query(openHelper_ob.BOOKING_TABLE_NAME, cols,null, null, null, null, null); 
     return c; 
    } 


    public int getMaxID() { 
     int id = 0; 
     opnToWrite(); 
     final String MY_QUERY = "SELECT MAX(_id) AS _id FROM onlinebooking"; 
     Cursor cursor = database_ob.rawQuery(MY_QUERY, null); 
     if (cursor.moveToFirst()) { 
      id = cursor.getInt(0); 
     } 
     Close(); 
     return id; 
    } 

} 

나를 도와 줄 수 있습니까?

09-03 05:01:42.880: E/AndroidRuntime(1016): FATAL EXCEPTION: main 
09-03 05:01:42.880: E/AndroidRuntime(1016): java.lang.NullPointerException 
090305:01:42.880:E/AndroidRuntime(1016):atcom.example.android.Online$1.onClick(Online.java:65) 
09-03 05:01:42.880: E/AndroidRuntime(1016):  at android.view.View.performClick(View.java:4204) 
09-03 05:01:42.880: E/AndroidRuntime(1016):  at android.view.View$PerformClick.run(View.java:17355) 

online.java이에서
package com.example.android; 

import java.util.Calendar; 

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.FragmentActivity; 
import android.text.format.DateFormat; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TimePicker; 

public class Online extends FragmentActivity { 
    private Spinner quantitySpinner; 
    static EditText editDateTime; 
    Button nextButton,cancelButton; 
    EditText nameEdit,numberEdit,piecesEdit,delivaryEdit,noteEdit; 
    BookingAdapter adapter; 
    BookingOpenHelper helper_ob; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.online); 

     nameEdit=(EditText) findViewById(R.id.nameEdit); 
     numberEdit=(EditText) findViewById(R.id.contactNumber); 

     quantitySpinner=(Spinner) findViewById(R.id.spinner); 

     piecesEdit=(EditText) findViewById(R.id.editpieces); 
     editDateTime=(EditText) findViewById(R.id.editdate); 
     delivaryEdit=(EditText) findViewById(R.id.editaddress); 
     noteEdit=(EditText) findViewById(R.id.editnote); 
     nextButton=(Button) findViewById(R.id.nextformbtn); 
     cancelButton=(Button) findViewById(R.id.cancelformbutton); 


     nextButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       String name=nameEdit.getText().toString(); 

       String contactnumber=numberEdit.getText().toString(); 

       String quantity = String.valueOf(quantitySpinner.getSelectedItem()); 

       String noofpieces=piecesEdit.getText().toString(); 

       String dateandtime=editDateTime.getText().toString(); 

       String delivaryaddress=delivaryEdit.getText().toString(); 

       String note=noteEdit.getText().toString(); 

       long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime, delivaryaddress, note); 



      } 
     }); 

     cancelButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent(Online.this,HomeScreen.class)); 
      } 
     }); 

     quantitySpinner = (Spinner) findViewById(R.id.spinner); 
     addListenerOnSpinnerItemSelection(); 
     editDateTime = (EditText) findViewById(R.id.editdate); 
     editDateTime.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       showTimePickerDialog(v); 
       showDatePickerDialog(v); 
      } 
     }); 
    } 

    public void showDatePickerDialog(View v) { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getSupportFragmentManager(), "datePicker"); 
    } 

    public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

     @Override 
     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); 

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

     @Override 
     public void onDateSet(DatePicker arg0, int year, int month, int day) { 
      // TODO Auto-generated method stub 
      editDateTime.setText(day + "/" + (month + 1) + "/" + year); 
     } 
    } 

    public void showTimePickerDialog(View v) { 
     DialogFragment newFragment = new TimePickerFragment(); 
     newFragment.show(getSupportFragmentManager(), "timePicker"); 
    } 

    public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      // Use the current time as the default values for the picker 
      final Calendar c = Calendar.getInstance(); 
      int hour = c.get(Calendar.HOUR_OF_DAY); 
      int minute = c.get(Calendar.MINUTE); 

      // Create a new instance of TimePickerDialog and return it 
      return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); 
     } 

     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      // Do something with the time chosen by the user 
      editDateTime.setText(editDateTime.getText() + " -" + hourOfDay + ":" + minute); 
     } 
    } 

    private void addListenerOnSpinnerItemSelection() { 
     quantitySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener()); 

    } 
} 
+0

다음 줄의 코드를 확인하십시오 : atcom.example.android.Online $ 1.onClick (Online.java:65). 그 Online.java 코드도 게시하십시오. – Santhosh

+0

online.java 파일을 추가했습니다 – Deepak

+0

65 번째 줄을 계산해야합니까? –

답변

0

adapter=new BookingAdapter (Context);//write this line starting of oncreate method 

long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime, delivaryaddress, note); 
+2

나는 당신이 당신의 대답과 함께 몇 가지 세부 사항을 제공해야한다고 생각합니다. –

+0

미래의 설명을 위해 .i는 내 대답을 편집했습니다 .. @ VipulPurohit – QuokMoon

+0

코드를 통해 변경하려는 사항을 말하고 싶습니다. –

0
long vals=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime, delivaryaddress, note); 

하나 또는 변수의 이상이 null 값을 반환하고 문제의 근본 이잖아 있습니다. 모든 변수의 값을 출력하면이 디버그가 오류로 연결됩니다.

+0

대답 주셔서 감사합니다 – Deepak

1

변경 사용하기 전에이 줄

개인 정적 최종 문자열 CREATE_BOOKING_TABLE를 "어댑터"의 인스턴스를 생성 = "테이블 onlinebooking (_id 정수 not null 기본 키 자동 증가, 이름 텍스트 널 (null)을 만들 , contactnumber 정수 null, 수량 텍스트 null, noofpcs 정수 null, dateandtime 텍스트 null, delivaryaddress 텍스트 null, 노트 텍스트 널) "; 이

개인 정적 최종 문자열 CREATE_BOOKING_TABLE = "테이블 onlinebooking (_id 정수 not null 기본 키 자동 증가, 이름 텍스트 널 (null), contactnumber 정수 널 (null), 수량 텍스트 널 (null)을 작성, noofpcs 정수로 선 위에

교체 null, dateandtime 텍스트 null, delivaryaddress 텍스트 null, 텍스트 null); ";

+0

No. 어댑터에 대한 인스턴스를 생성 한 후 잘 작동합니다. 응답 해 주셔서 감사합니다. – Deepak

+0

@ user2666917 Ok :) – PrashantAdesara

관련 문제