package my.honam.Datamanager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;
public class ContentProviderTest extends ContentProvider {
static final Uri CONTENT_URI = Uri.parse("content://my.honam.DataManager.DBTest/word");
static final int ALLWORD =1;
static final int ONEWORD=2;
static final UriMatcher matcher;
static{
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("my.honam.DataManager.DBTest", "word", ALLWORD);
matcher.addURI("my.honam.DataManager.DBTest", "word/*", ONEWORD);
}
SQLiteDatabase db;
@Override
//도우미크래스
public boolean onCreate() {
DBHelper helper = new DBHelper(getContext());
db = helper.getWritableDatabase();
return true;
}
@Override
//MIME 타입을 정의
public String getType(Uri uri) {
if(matcher.match(uri)==ALLWORD){
return "vnd.DBTest.Data.cursor.item/word";
}
if(matcher.match(uri)==ONEWORD){
return "vnd.DBTest.Data.cursor.dir/words";
}
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String sql;
//전체레코드 추출
sql = "select * from student";
//조건에 맞는 레코드 추출
if(matcher.match(uri) == ONEWORD){
sql += " where name = '" + uri.getPathSegments().get(1) + "'";
}
Cursor cursor = db.rawQuery(sql, null);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long row = db.insert("student", null, values);
if(row > 0){
Uri notiuri = ContentUris.withAppendedId(CONTENT_URI, row);
getContext().getContentResolver().notifyChange(notiuri, null);
return notiuri;
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count=0;
switch(matcher.match(uri)){
case ALLWORD:
count = db.delete("student", selection, selectionArgs);
break;
case ONEWORD:
String where;
where = "name = '" + uri.getPathSegments().get(1) + "'";
if(TextUtils.isEmpty(selection) == false){
where += " and" + selection;
}
count = db.delete("student", where, selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
switch (matcher.match(uri)) {
case ALLWORD:
count = db.update("student", values, selection, selectionArgs);
break;
case ONEWORD:
String where;
where = "name = '" + uri.getPathSegments().get(1) + "'";
if(TextUtils.isEmpty(selection)==false){
where += " AND " + selection;
}
count = db.update("student", values, where, selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context){
super(context, "Management.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(id integer primary key autoincrement, "+"name text, age integer);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists student";
db.execSQL(sql);
onCreate(db);
}
}
11-17 23:57:13.865: ERROR/BatteryService(53): usbOnlinePath not found
11-17 23:57:13.875: ERROR/BatteryService(53): batteryVoltagePath not found
11-17 23:57:13.875: ERROR/BatteryService(53): batteryTemperaturePath not found
11-17 23:57:14.006: ERROR/SurfaceFlinger(53): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
11-17 23:57:21.780: ERROR/PackageManager(53): Package my.honam has mismatched uid: 10026 on disk, 10027 in settings; read messages:
11-17 23:57:21.780: ERROR/PackageManager(53): Read completed successfully: 47 packages, 7 shared uids
11-17 23:57:23.235: ERROR/EventHub(53): could not get driver version for /dev/input/mouse0, Not a typewriter
11-17 23:57:23.235: ERROR/EventHub(53): could not get driver version for /dev/input/mice, Not a typewriter
11-17 23:57:23.486: ERROR/System(53): Failure starting core service
11-17 23:57:23.486: ERROR/System(53): java.lang.SecurityException
11-17 23:57:23.486: ERROR/System(53): at android.os.BinderProxy.transact(Native Method)
11-17 23:57:23.486: ERROR/System(53): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
11-17 23:57:23.486: ERROR/System(53): at android.os.ServiceManager.addService(ServiceManager.java:72)
11-17 23:57:23.486: ERROR/System(53): at com.android.server.ServerThread.run(SystemServer.java:176)
11-17 23:57:23.515: ERROR/AndroidRuntime(53): Crash logging skipped, no checkin service
11-17 23:57:44.979: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
11-17 23:57:44.979: ERROR/MediaPlayer(53): Unable to to create media player
11-17 23:57:46.710: ERROR/ActivityThread(103): Failed to find provider info for android.server.checkin
11-17 23:57:52.790: ERROR/ActivityThread(103): Failed to find provider info for android.server.checkin
11-17 23:57:52.979: ERROR/ActivityThread(103): Failed to find provider info for android.server.checkin
11-17 23:58:09.569: ERROR/AndroidRuntime(122): ERROR: thread attach failed
11-17 23:58:12.350: ERROR/ActivityThread(53): Failed to find provider info for com.google.settings
11-17 23:58:12.350: ERROR/ActivityThread(53): Failed to find provider info for com.google.settings
11-17 23:58:15.829: ERROR/PackageManager(53): Package my.honam has mismatched uid: 10026 on disk, 10027 in settings
11-17 23:58:17.050: ERROR/AndroidRuntime(225): ERROR: thread attach failed
11-17 23:58:23.080: ERROR/AndroidRuntime(246): ERROR: thread attach failed
11-17 23:58:24.410: ERROR/AndroidRuntime(257): Uncaught handler: thread main exiting due to uncaught exception
11-17 23:58:24.440: ERROR/AndroidRuntime(257): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.honam.Datamanager/my.honam.Datamanager.ContentProviderTest}: java.lang.ClassCastException: my.honam.Datamanager.ContentProviderTest
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.os.Looper.loop(Looper.java:123)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at java.lang.reflect.Method.invoke(Method.java:521)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at dalvik.system.NativeStart.main(Native Method)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): Caused by: java.lang.ClassCastException: my.honam.Datamanager.ContentProviderTest
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
11-17 23:58:24.440: ERROR/AndroidRuntime(257): ... 11 more
11-17 23:58:57.720: ERROR/ActivityThread(53): Failed to find provider info for android.server.checkin
11-17 23:59:08.590: ERROR/AndroidRuntime(270): Uncaught handler: thread main exiting due to uncaught exception
11-17 23:59:08.609: ERROR/AndroidRuntime(270): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.honam.Datamanager/my.honam.Datamanager.ContentProviderTest}: java.lang.ClassCastException: my.honam.Datamanager.ContentProviderTest
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.os.Looper.loop(Looper.java:123)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at java.lang.reflect.Method.invoke(Method.java:521)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at dalvik.system.NativeStart.main(Native Method)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): Caused by: java.lang.ClassCastException: my.honam.Datamanager.ContentProviderTest
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
11-17 23:59:08.609: ERROR/AndroidRuntime(270): ... 11 more
---------------------------------------------------------------------------------------------
원인이 뭘까요..... 소스엔 에러가 없는데 실행이 안됩니다 ㅠㅠ
이것도 메니피스트 같은 느낌인데요....
my.honam.Datamanager/my.honam.Datamanager.ContentProviderTest
메니피스트 등록하실때 Pakage를 my.honam.Datamanager로 주셨으니까
".ContentProviderTest" 형태로만 쓰시면 됩니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.honam.Datamanager"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContentProviderTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="CallCP"></activity>
</application>
<provider android:name="ContentProviderTest"
android:authorities="my.honam.Datamanager.ContentProviderTest" />
<uses-sdk android:minSdkVersion="7" />
</manifest>
이렇게 했는데 안됩니다 ㅠㅠ
우선 공지사항 먼저 보시고..질문을 올려주세요.
어디서 문제가 났는지는 올려주신 logcat 에 보면 소스코드랑 라인까지 찍힙니다.
거기에 브레이크 포인터 걸고 디버깅 모드로 실행해서 왜 에러가 났는지 보시면 됩니다..
보니까 ClassCastException 이 많이 보이는데, 형변환을 못하는 것을 강제로 시킨게 아닐까 싶기도 하네요.