在Android中从构造函数创建对象时遇到问题

时间:2015-05-27 17:13:00

标签: java android sqlite android-fragments constructor

我是一个相对较新的开发人员,并且已经在一个项目上工作了一段时间。我一直在尝试使用我可以插入的SQLite数据库以及稍后查看(但是我会担心稍后查看条目)。我以为我已经完成了所有工作但是当我尝试从我的数据库类的构造函数中创建一个对象时,它不允许我使用我需要的参数。我也在使用碎片,如果这很重要,因为我觉得这可能是问题的一部分。我将不胜感激任何帮助。我不会展示我的整个申请,除非你们认为这是必要的,但我会展示所有涉及的课程。如果你们能提供帮助,请告诉我。

First Class(DatabaseManager):

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

import java.sql.SQLException;

public class DatabaseManager{
    public Context mAppContext;
    private SQLiteDatabase mDataBase;
    private PhotoGalleryDatabase mHelper;
    private static final String DB_NAME = "vacations.sqlite";
    private static final int VERSION = 1;
    private static final String VACATION_TABLE = "vacation_entries";

    public static final String VACATION_ID = "vacationID";
    public static final String VACATION_DATE = "vacationDate";
    public static final String VACATION_LOCATION = "vacationLocation";
    public static final String VACATION__DESCRIPTION = "description";
    public static final String VACATION_URL = "picURL";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";



    public DatabaseManager(Context appContext) {
        this.mAppContext = appContext;
        mHelper = new PhotoGalleryDatabase(mAppContext);
    }


    private static class PhotoGalleryDatabase extends SQLiteOpenHelper {


        public PhotoGalleryDatabase(Context context){
            super(context, DB_NAME, null, VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db){
            //Create the VacationEntries Table
            db.execSQL("CREATE TABLE " + VACATION_TABLE + " (" + VACATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    VACATION_DATE + " TEXT, " + VACATION_LOCATION +  " TEXT, " + VACATION__DESCRIPTION + " TEXT, " + LATITUDE + " REAL, " + LONGITUDE + " REAL" + VACATION_URL + " BLOB);");


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
            db.execSQL("DROP TABLE IF EXISTS " + VACATION_TABLE);
            onCreate(db);

        }
    }





    public DatabaseManager open() throws SQLException{
        mDataBase = mHelper.getWritableDatabase();
        return this;
    }

    public long createEntry(String location, String date, String latitude, String longitude, String description) {
        ContentValues cv = new ContentValues();
        cv.put(VACATION_LOCATION, location);
        cv.put(VACATION_DATE, date);
        cv.put(VACATION__DESCRIPTION, description);
        cv.put(LATITUDE, latitude);
        cv.put(LONGITUDE, longitude);
        return mDataBase.insert(VACATION_TABLE, null, cv);

    }

    public void close(){
        mHelper.close();
    }

}



Second Class (Fragment):


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.student.photogallery2.R;

import java.sql.SQLException;

public class DatabaseFormFragment extends VisibleFragment{
    EditText description_editText, longitude_editText, latitude_editText, location_editText;
    EditText datePicker;
    Button submit_button;




    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.vacation_photo_form, container, false);

        description_editText = (EditText)v.findViewById(R.id.description_editText);
        longitude_editText = (EditText)v.findViewById(R.id.longitude_editText);
        latitude_editText = (EditText)v.findViewById(R.id.latitude_editText);
        datePicker = (EditText)v.findViewById(R.id.datePicker);
        location_editText = (EditText)v.findViewById(R.id.location_editText);

        submit_button= (Button)v.findViewById(R.id.submit_button);
        submit_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String location = location_editText.getText().toString();
                String date = datePicker.getText().toString();
                String latitude = latitude_editText.getText().toString();
                String longitude = longitude_editText.getText().toString();
                String description = description_editText.getText().toString();


                DatabaseManager entry = new DatabaseManager(DatabaseFormFragment.this);
                try {
                    entry.open();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                entry.createEntry(location, date, latitude, longitude, description);
                entry.close();




            }
        });

        return v;
    }


}

    enter code here

1 个答案:

答案 0 :(得分:0)

问题是您的构造函数需要Context对象,而Fragment类不会扩展Context。我相信你的修复正在取代以下一行:

DatabaseManager entry = new DatabaseManager(DatabaseFormFragment.this);

DatabaseManager entry = new DatabaseManager(getActivity());