2013-08-07 36 views
1

我想要獲得Android教程的SQLite3數據庫訪問示例http://developer.android.com/training/basics/data-storage/databases.html。我陷在一條錯誤消息,我無法理解:Android教程數據庫訪問

構造FeedReaderDbHelper(上下文)是指缺少類型 語境

我不知道爲什麼編譯器會抱怨缺少Context類型或者如何解決錯誤。錯誤報告在第一個文件的第mDbHelper = new FeedReaderDbHelper(c);行。這裏是源文件:

文件1:MainActivity.java

package com.example.sqlitetest; 

import android.os.Bundle; 
import android.database.sqlite.*; 
import android.app.Activity; 
import android.view.*; 
import android.content.*; 

public class MainActivity extends Activity { 
    FeedReaderDbHelper mDbHelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View v = findViewById(R.layout.activity_main); 
     Context c = v.getContext(); 
     mDbHelper = new FeedReaderDbHelper(c); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

文件2:FeedReaderContract

package com.example.sqlitetest; 

import android.provider.BaseColumns; 
import android.database.sqlite.*; 
import android.content.*; 

public final class FeedReaderContract { 
     // To prevent someone from accidentally instantiating the contract class, 
    // give it an empty constructor. 
    public FeedReaderContract() {} 

    /* Inner class that defines the table contents */ 
    public static abstract class FeedEntry implements BaseColumns { 
     public static final String TABLE_NAME = "entry"; 
     public static final String COLUMN_NAME_ENTRY_ID = "entryid"; 
     public static final String COLUMN_NAME_TITLE = "title"; 
     public static final String COLUMN_NAME_SUBTITLE = "subtitle"; 
//  ... 
    } 

    private static final String TEXT_TYPE = " TEXT"; 
    private static final String COMMA_SEP = ","; 
    private static final String SQL_CREATE_ENTRIES = 
     "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" + 
     FeedEntry._ID + " INTEGER PRIMARY KEY," + 
     FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP + 
     FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP + 
//  ... // Any other options for the CREATE command 
     ")"; 

    private static final String SQL_DELETE_ENTRIES = 
     "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME; 

    public class FeedReaderDbHelper extends SQLiteOpenHelper { 
     // If you change the database schema, you must increment the database version. 
     public static final int DATABASE_VERSION = 1; 
     public static final String DATABASE_NAME = "FeedReader.db"; 

     public FeedReaderDbHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(SQL_CREATE_ENTRIES); 
     } 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // This database is only a cache for online data, so its upgrade policy is 
      // to simply to discard the data and start over 
      db.execSQL(SQL_DELETE_ENTRIES); 
      onCreate(db); 
     } 
     public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      onUpgrade(db, oldVersion, newVersion); 
     } 
    } 
} 

回答

1

使用mDbHelper = new FeedReaderDbHelper(this); or mDbHelper = new FeedReaderDbHelper(getApplicationContext());

所以你onCreate()應該是

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mDbHelper = new FeedReaderDbHelper(this); 
    } 

2.

使用extends SQLiteOpenHelper這主要類,而不是在子類,所以更改爲public class FeedReaderContract extends SQLiteOpenHelper

make constructor of FeedReaderContractthan pass context to constructor of FeedReaderContract

以及所有其他C ode/functionality of FeedReaderDbHelper move to FeedReaderContract

+0

我粘貼了你的改變(都是),但是t他的錯誤依然如此:-( – user1443098

+0

老鼠!那也行不通。 – user1443098

+0

修正了它!我有FeedReaderDbHelper作爲子類。我把它搬到了自己的班級,問題消失了 – user1443098