package com.zj.sql;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class Sql1 extends ListActivity {
/** Called when the activity is first created. */

public static int g_variable;
public static final String AUTHORITY = "com.zj.provider.sql";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "StudentTable"); private static final String[] PROJECTION = new String[] { "stud_no",
"stud_name" }; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(this.getClass().toString(), "22");
Intent intent = getIntent();
if (intent.getData() == null) {
intent.setData(CONTENT_URI);
Cursor cur = getContentResolver().query(getIntent().getData(),
PROJECTION, null, null, null);
ArrayList<Map<String, Object>> coll = new ArrayList<Map<String, Object>>();
Map<String, Object> item;
cur.moveToFirst();
while (!cur.isAfterLast()) {
item = new HashMap<String, Object>();
item.put("cl", cur.getString(0) + "," + cur.getString(1));
coll.add(item);
cur.moveToNext();
}
this.setListAdapter(new SimpleAdapter(this, coll,
android.R.layout.simple_list_item_1, new String[] { "c1" },
new int[] { android.R.id.text1 }));
}

}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
finish();
}
}package com.zj.sql;import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;public class DataProvider extends ContentProvider {

DataProvider(){
Log.e(this.getClass().toString(), "0000");
}
private static final String DATABASE_NAME = "StudDB_2.db";
private static final int DATABASE_VERSION = 2;
private static final String TABLE_NAME = "StudentTable";

private static class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
} @Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.e(this.getClass().toString(), "11");
db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + "stud_no" + "TEXT,"
+ "stud_name" + "Text" + ");");
String sql1 = "insert into " + TABLE_NAME
+ "(stud_no,stud_name)values('s100','a')";
String sql2 = "insert into " + TABLE_NAME
+ "(stud_no,stud_name)values('s101','b')";
String sql3 = "insert into " + TABLE_NAME
+ "(stud_no,stud_name)values('s102','c')";
try {
db.execSQL(sql1);
db.execSQL(sql2);
db.execSQL(sql3);
} catch (SQLException e) {
Log.e(this.getClass().toString(), e.toString());
}
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub } } private DatabaseHelper mOpenHelper; @Override
public boolean onCreate() {
// TODO Auto-generated method stub
mOpenHelper = new DatabaseHelper(getContext());
return true;
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = db.query(TABLE_NAME, projection, null, null, null, null,
null);
return c;
} @Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
} @Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
} @Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return uri;
} @Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zj.sql"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <provider android:name="DataProvider"
                  android:authorities="com.zj.provider.sql">
                  </provider>
        
        <activity android:name=".Sql1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>    </application>
</manifest> 
错误信息:
06-19 06:35:54.932: ERROR/AndroidRuntime(416): FATAL EXCEPTION: main
06-19 06:35:54.932: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to get provider com.zj.sql.DataProvider: java.lang.IllegalAccessException: access to constructor not allowed
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.installProvider(ActivityThread.java:4509)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.installContentProviders(ActivityThread.java:4281)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4237)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.access$3000(ActivityThread.java:125)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.os.Looper.loop(Looper.java:123)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at java.lang.reflect.Method.invoke(Method.java:521)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at dalvik.system.NativeStart.main(Native Method)
06-19 06:35:54.932: ERROR/AndroidRuntime(416): Caused by: java.lang.IllegalAccessException: access to constructor not allowed
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at java.lang.Class.newInstanceImpl(Native Method)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at java.lang.Class.newInstance(Class.java:1429)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.installProvider(ActivityThread.java:4494)
06-19 06:35:54.932: ERROR/AndroidRuntime(416):     ... 12 more

解决方案 »

  1.   

    06-19 06:35:54.932: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to get provider com.zj.sql.DataProvider: java.lang.IllegalAccessException: access to constructor not allowed
    从log上面看,没有访问provider的权限。android:authorities="com.zj.provider.sql"  这句去掉试试。
      

  2.   

    我错了,是构造函数无法访问。1. 将DataProvider改为public的试试吧.
    2. 将android:authorities="com.zj.provider.sql"改为android:authorities="com.zj.sql.DataProvider"或者
    android:authorities="com.zj.sql.dataProvider"
      

  3.   


    确实是构造器的问题 ,改了以后好了,不过出现其他的问题了 
    理论上覆盖的 DatabaseHelper 的 onCreate() 方法应该自动调用,可是我验证了半天怎么都不调用,以导致了后边的错误,能帮忙想想为什么吗?
      

  4.   

    楼主的Manifest有问题。android:authorities="com.zj.provider.sql" 这句去掉
    改成
     <provider android:name=".DataProvider">
      </provider>
    注意上面有个.试试。有问题再回复啊。