Android代码从ContentProvider类开始而不是Main类

时间:2011-06-08 09:00:56

标签: android sqlite android-contentprovider android-context

昨天我发布了一个类似的问题,为什么我的代码开始在我的内容提供程序类而不是主类上,我得到了一些我已经更新的反馈,但问题仍然存在于代码从ContentProvider类开始的地方比主要班级。我已经使用调试器运行代码,并且令人费解的是为什么代码在Content Provider中启动并在Context阶段传递回主类。我希望在这里寻求一些帮助!

主要课程在这里:

public class MedF1 extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drug_list); 

    ListView drugListView;
    ArrayAdapter<Drug> aa;
    ArrayList<Drug> drugs = new ArrayList<Drug>();

    drugListView = (ListView)this.findViewById(R.id.list1);
    DrugProvider.DatabaseHelper mDbHelper1 = new DrugProvider.DatabaseHelper(this);

    //Creation of the Database here
    try {

    mDbHelper1.createDataBase();

    } catch (IOException ioe) {

    throw new Error("Unable to create database");

    }

    try {

    mDbHelper1.openDataBase();

    }catch(SQLException sqle){

    throw sqle;

    }

Content Provider类如下。

public class DrugProvider extends ContentProvider {

// publishing the URI for this provider
public static final Uri CONTENT_URI = Uri
        .parse("content://com.paad.provider.drug/drugs");

private static final int DRUGS = 1;
private static final int DRUG_ID = 2;
private static final UriMatcher uriMatcher;

// allocating UriMatcher object

static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("com.paad.provider.drug", "drugs", DRUGS);
    uriMatcher.addURI("com.paad.provider.drug", "drugs/#", DRUG_ID);
}

// Column names
public static final String KEY_ROWID = "_id";
public static final String KEY_DRUG = "drug";
public static final String KEY_CONTENT = "content";
public static final String KEY_INDICATION = "indication";
public static final String KEY_DOSAGE = "dosage";
public static final String KEY_SPECIALPRECAUTION = "specialprecaution";

// Column indexes
public static final int DRUG_COLUMN = 1;
public static final int CONTENT_COLUMN = 2;
public static final int INDICATION_COLUMN = 3;
public static final int DOSAGE_COLUMN = 4;
public static final int SPECIALPRECAUTION_COLUMN = 5;

// private DatabaseHelper mDbHelper;
// private SQLiteDatabase mDb;
private static String DB_PATH = "data/data/com.paad.MedF1/databases/";
private static String DB_NAME = "data";
private static final int DATABASE_VERSION = 1;
private static final String DRUG_TABLE = "drugs";

public static SQLiteDatabase myDataBase;



// Creation of the database and its basic parameters
public static class DatabaseHelper extends SQLiteOpenHelper {   

    public final Context myContext;

    public DatabaseHelper(Context context) {

        super(context, DB_NAME, null, 1);

        this.myContext = context;

    }

2 个答案:

答案 0 :(得分:1)

嘿,我已经找到了代码在ContentProvider而不是主类开始的原因。这是因为ContentProvider扩展附带的实现方法(onCreate)。我重新调整了另一个教程来创建它而没有真正理解它......这就是为什么。是的,任何面临问题的人都应该尝试探索内容提供者类中的onCreate方法是否影响它 - 它不是明显的问题:)

答案 1 :(得分:0)

感谢您提供该信息,调试器对于清单问题无效,您无法在清单中添加日志条目或Toast。