gpt4 book ai didi

java - Android 构造函数未定义

转载 作者:行者123 更新时间:2023-12-02 05:30:27 25 4
gpt4 key购买 nike

我正在尝试为 Android 中的每个滑动 View 执行不同的 SQL 语句。这是我的 onCreateView 的一部分:

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

expensesCat = inflater.inflate(R.layout.dashboard_expensescat_frag,
container, false);

DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());

trans_list = trc.getChartExpensesCat();
openChart();
mDbHelper.close();
return expensesCat;
}

在我的 Controller 类中:

private SQLiteDatabase mDb;
public TransactionRecController(SQLiteDatabase mDb) {
this.mDb = mDb;
}

public ArrayList<TransactionRecModel> getChartExpensesCat() {
try {
String sql = "SELECT SUM(tr.amount) AS total, c.categoryName FROM transactionRec tr " +
"INNER JOIN category c ON tr.categoryID = c.categoryID " +
"WHERE tr.type = 'W' GROUP BY c.categoryName";
Cursor mCur = mDb.rawQuery(sql, null);
Log.e(TAG, "Data Grab Success");
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
TransactionRecModel trm = new TransactionRecModel();

trm.setCategory(mCur.getString(mCur
.getColumnIndex("categoryName")));
trm.setAmount(mCur.getInt(mCur.getColumnIndex("total")));

transList.add(trm);
} while (mCur.moveToNext());
}
}
return transList;
} catch (SQLException mSQLException) {
throw mSQLException;
}
}

但是,当我尝试初始化 DatabaseAdapter 时,我收到此错误消息:

The constructor DatabaseAdapter(DashboardExpensesCat) is undefined

我想知道如何修复此错误,因为我无法更改 Controller 类中的构造函数。

提前致谢。

最佳答案

问题是您正在传递 fragment 的对象

DatabaseAdapter mDbHelper = new DatabaseAdapter(this);

这里的this表示Fragment的实例或对象。在这里,您必须传递 Activity 的 Context ,并且当您在 Fragment 中初始化它时,您需要传递

DatabaseAdapter mDbHelper = new DatabaseAdapter(getActivity());

关于java - Android 构造函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25598996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com