从ContentProvider类的Andr​​oid code出发,而不是主类而不是、Andr、ContentProvider、code

2023-09-04 06:02:08 作者:待我强大自给天下

昨天我张贴的,为什么我的code开始了我的内容提供者类,而不是主类类似的问题,我已经得到了一些反馈,我已经更新了,但问题仍然存在,其中code开始ContentProvider类,而不是主类。我已经通过了code与调试运行,其令人费解,为什么code开头的内容提供商,并传回主类的语境阶段。我希望在这里听取一些帮助!

主类是在这里:

 公共类MedF1延伸活动{

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.drug_list);

    ListView控件drugListView;
    ArrayAdapter<药物> AA;
    ArrayList的<药物>药品=新的ArrayList<药物及GT;();

    drugListView =(ListView控件)this.findViewById(R.id.list1);
    DrugProvider.DatabaseHelper mDbHelper1 =新DrugProvider.DatabaseHelper(本);

    //创建数据库的位置
    尝试 {

    mDbHelper1.createDataBase();

    }赶上(IOException异常IOE){

    抛出新的错误(无法创建数据库);

    }

    尝试 {

    mDbHelper1.openDataBase();

    }赶上(的SQLException SQLE){

    扔SQLE;

    }
 

内容提供者类如下。

 公共类DrugProvider扩展ContentProvider的{

//发布的URI这个供应商
公共静态最终乌里CONTENT_URI =开放的我们
        .parse(内容://com.paad.provider.drug/drugs);

私有静态最终诠释药物= 1;
私有静态最终诠释DRUG_ID = 2;
私有静态最后UriMatcher uriMatcher;

//分配UriMatcher对象

静态{
    uriMatcher =新UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(com.paad.provider.drug,药,药);
    uriMatcher.addURI(com.paad.provider.drug,药/#,DRUG_ID);
}

//列名
公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_DRUG =药;
公共静态最后弦乐KEY_CONTENT =内容;
公共静态最后弦乐KEY_INDICATION =指示;
公共静态最后弦乐KEY_DOSAGE =用量;
公共静态最后弦乐KEY_SPECIAL preCAUTION =特殊的precaution;

//列索引
公共静态最终诠释DRUG_COLUMN = 1;
公共静态最终诠释CONTENT_COLUMN = 2;
公共静态最终诠释INDICATION_COLUMN = 3;
公共静态最终诠释DOSAGE_COLUMN = 4;
公共静态最终诠释SPECIAL preCAUTION_COLUMN = 5;

//私人DatabaseHelper mDbHelper;
//私人SQLiteDatabase MDB;
私有静态字符串DB_PATH =数据/数据​​/ com.paad.MedF1 /数据库/;
私有静态字符串DB_NAME =数据;
私有静态最终诠释DATABASE_VERSION = 1;
私有静态最后弦乐DRUG_TABLE =药品;

公共静态SQLiteDatabase MyDatabase的;



//创建的数据库和它的基本参数
公共静态类DatabaseHelper扩展SQLiteOpenHelper {

    公众最终上下文myContext;

    公共DatabaseHelper(上下文的背景下){

        超级(上下文,DB_NAME,空,1);

        this.myContext =背景;

    }
 

解决方案

嘿,我已经找到了,为什么code开始在ContentProvider的,而不是主类。这是因为实现的方法(的onCreate)随附的ContentProvider的延伸。我另作它用另一个教程来创建它并没有真正理解它...这就是为什么。烨其他人所面临的问题,应该尝试探索,如果在内容提供类的onCreate方法是影响它 - 它不是明显的问题:)

Yesterday I posted a similar question on why my code started on my content provider class rather than the main class and I've gotten some feedback which I've updated but the problem still remains where the code starts with the ContentProvider class rather than the main class. I've run through the code with debugger and its puzzling why the code starts in the Content Provider and passes back to the main class at the Context stage. I hope to solicit some help here !

The main class is here:

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;

    }

The Content Provider class is as follows.

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;

    }

解决方案

Hey I've found out why the code started at the ContentProvider rather than the main class. It was because of the implemented method (onCreate) that comes with the extension of the ContentProvider . I repurposed another tutorial to create it without really understanding it ... thats why. yup anyone else facing the problem should try exploring if the onCreate method in the Content provider class is affecting it - its not the manifest problem :)