我有這個類,省略了代碼,使其更易於閱讀:是否有任何類在Android中被視爲Context或者是否有必須完成的具體事情?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
public class TimeTrackerActivity extends Activity {
//some code here
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.times_list);
listView.setAdapter(timeTrackerAdapter);
TimeTrackerOpenHelper openHelper = new TimeTrackerOpenHelper(this);
//my error with line of code right above saying
//the constructor of TimeTrackerOpenHelper(TimeTrackerActivity) is undefined
}
//more code here
}
我評論在我的錯誤。我在網上查看了其他示例,並且其他人都將其發送到擴展SQLiteOpenHelper的類,這導致我認爲它應該工作。但不是。我的代碼如下:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TimeTrackerOpenHelper extends SQLiteOpenHelper {
TimeTrackerOpenHelper(Context context) {
super(context, "timetracker.db", null, 1);
}
public void onCreate(SQLiteDatabase database) {/*stuff*/}
public void onUpgrade(SQLiteDatabase datbase, int oldVersion, int newVersion) {/*stuff*/}
}
我的假設是錯誤的,是否有我缺少的東西。
您可能需要將TimeTrackerOpenHelper構造函數設爲public。 – superfell