0

我正在为我的小组项目制作一个 android 应用程序并收到以下错误:

The constructor GradesDataSource(AddGradesFragment) is undefined

这是包含错误的代码:

package com.petroc.nationaldiploma;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.actionbarsherlock.app.SherlockFragment;

public class AddGradesFragment extends SherlockFragment {

private GradesDataSource datasource;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    datasource = new GradesDataSource(this);


    Spinner spinner;

    View view = inflater.inflate(R.layout.fragment_add_grades, container,
            false);

    spinner = (Spinner) view.findViewById(R.id.spinner);
    List<String> list = new ArrayList<String>();
    list.add("Pass");
    list.add("Merit");
    list.add("Distinction");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_spinner_item, list);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);

    spinner = (Spinner) view.findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    // EditText editText = (EditText)
    // view.findViewById(R.id.editModuleText1);

    return view;
}
}

这是 GradesDataSource 类:

package com.petroc.nationaldiploma;

import java.util.ArrayList;
import java.util.List;

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

public class GradesDataSource {

// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
        MySQLiteHelper.COLUMN_GRADE, MySQLiteHelper.COLUMN_MODULE };

public GradesDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

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

public Grade createGrade(String grade, String module) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_GRADE, grade);
    values.put(MySQLiteHelper.COLUMN_MODULE, module);
    long insertId = database.insert(MySQLiteHelper.TABLE_GRADES, null, 
            values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, 
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
            null, null, null);
    cursor.moveToFirst();
    Grade newGrade = cursorToGrade(cursor);
    cursor.close();
    return newGrade;
}

public void deleteGrade(Grade grade) {
    long id = grade.getId();
    System.out.println("Grade deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_GRADES, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);
}

public List<Grade> getAllGrades() {
    List<Grade> grades = new ArrayList<Grade>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, 
            allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Grade grade = cursorToGrade(cursor);
        grades.add(grade);
          cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return grades;
}

private Grade cursorToGrade(Cursor cursor) {
    Grade grade = new Grade();
    grade.setId(cursor.getLong(0));
    grade.setGrade(cursor.getString(1));
    return grade;
}

  }

为什么我会收到此错误?我使用了本教程:http ://www.vogella.com/articles/AndroidSQLite/article.html

生成的应用程序运行良好,我现在将此代码添加到我的小组项目的应用程序中

4

2 回答 2

3

更改此行:

 datasource = new GradesDataSource(this);

 datasource = new GradesDataSource(getActivity());

您正在传递对没有构造函数的 Fragment 的引用。您想要传递通过该getActivity()方法获得的 Host Activity 的上下文。

于 2013-03-25T13:07:15.240 回答
0

代替

datasource = new GradesDataSource(this);

你应该使用

datasource = new GradesDataSource(getActivity());
于 2013-03-25T13:08:21.747 回答