Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Content Provider Errors
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appex.android.inquisitor" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_start" >
</activity>
<activity
android:name=".AboutActivity"
android:parentActivityName=".QuestionActivity"
android:theme="@style/Dialogtheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".QuestionActivity" />
</activity>
<activity
android:name=".QuestionActivity"
android:label="@string/title_activity_question" >
</activity>
<activity
android:name=".StartActivity"
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=".EndActivity"
android:label="@string/title_activity_end" >
</activity>
<activity
android:name=".GoogleActivity"
android:label="@string/title_activity_google" >
</activity>
<activity
android:name=".QuesDispActivity"
android:label="@string/title_activity_ques_disp" >
</activity>
<provider android:name=".QuestionsProvider"
android:authorities="com.appex.android.inquisitor.QuestionProvider">
</provider>
</application>
</manifest>
05-28 13:42:12.956 3268-3268/com.appex.android.inquisitor E/ActivityThread﹕ Failed to find provider info for com.appex.android.inquisitor.QuestionsProvdider
05-28 13:42:12.966 3268-3268/com.appex.android.inquisitor E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appex.android.inquisitor, PID: 3268
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3954)
at android.view.View.performClick(View.java:4569)
at android.view.View$PerformClick.run(View.java:18570)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3949)
            at android.view.View.performClick(View.java:4569)
            at android.view.View$PerformClick.run(View.java:18570)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.appex.android.inquisitor.QuesDispActivity.onClickRetrieveQuestions(QuesDispActivity.java:69)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3949)
            at android.view.View.performClick(View.java:4569)
            at android.view.View$PerformClick.run(View.java:18570)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
            at dalvik.system.NativeStart.main(Native Method)
package com.appex.android.inquisitor;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class QuesDispActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences count = getSharedPreferences(MainFragment.PREFS_FILE, 0);
int mcount = count.getInt("count", MainFragment.mstartcount);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ques_disp);
getSupportActionBar().hide();
}
String ques[] = {
"FD,RT,LT,BK,Turtle.",
"Sixth Sense on Paper.",
"White House, Jenny Craig Inc.",
"11, Oranje.",
"Perkins Engineering, Brown-Forman.",
"Whenever windows opened, it was there. But it will soon disappear.",
"Grand Theft Auto, Ranbir Kapoor.",
"Hoomerpalooza, Lollapalooza.",
"Was used in war, now a TV star.",
"Juiced by the National Research Corporation"
};
public void onClickAddQues(View view) {
for (int i = 0; i < ques.length; i++) {
ContentValues values = new ContentValues();
values.put(QuestionsProvider.QUESTION,
(ques[i]));
Uri uri = getContentResolver().insert(
QuestionsProvider.CONTENT_URI, values);
Toast.makeText(getBaseContext(),
uri.toString(), Toast.LENGTH_LONG).show();
}
}
//TextView qView = (TextView) findViewById(R.id.qview);
//Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Infinity.ttf");
//qView.setTypeface(typeface);
public void onClickRetrieveQuestions(View view) {
// Retrieve student records
String URL = "content://com.appex.android.inquisitor.QuestionsProvdider/questions";
Uri students = Uri.parse(URL);
Cursor c = managedQuery(students, null, null, null, "name");
String LOGD="";
Log.d(LOGD,"you are here");
if (c.moveToFirst()) {
do {
Toast.makeText(this,
c.getString(c.getColumnIndex(QuestionsProvider._ID)) +
", " + c.getString(c.getColumnIndex(QuestionsProvider.QUESTION)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
}
package com.appex.android.inquisitor;
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.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import java.util.HashMap;
public class QuestionsProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.appex.android.inquisitor.QuestionProvider";
static final String URL = "content://com.appex.android.inquisitor.QuestionProvider/questions";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String QUESTION="question";
static final String _ID = "_id";
private static HashMap<String, String> QUESTIONS_PROJECTION_MAP;
static final int QUESTIONS = 1;
static final int QUESTION_ID = 2;
static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "questions", QUESTIONS);
uriMatcher.addURI(PROVIDER_NAME, "questions/#", QUESTION_ID);
}
private SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "QuizDB";
private static final String TABLE_QUESTION = "question";
private static final String KEY_ID = "id";
private static final String KEY_QUESTION = "question";
private static final String KEY_CHK = "chk";
private static final String[] COLUMNS = {KEY_ID, KEY_QUESTION, KEY_CHK};
public class QuizDatabaseHelper extends SQLiteOpenHelper {
public QuizDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE question (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS question");
this.onCreate(db);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
QuizDatabaseHelper dbHelper = new QuizDatabaseHelper(context);
db = dbHelper.getWritableDatabase();
return (db == null)? false:true;
}
@Override
public Uri insert(Uri uri,ContentValues values) {
Quiz qa=new Quiz();
values.put(KEY_QUESTION, qa.getQues());
long rowID=db.insert(TABLE_QUESTION, null, values);
if (rowID > 0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLE_QUESTION);
switch (uriMatcher.match(uri)) {
case QUESTIONS:
qb.setProjectionMap(QUESTIONS_PROJECTION_MAP);
break;
case QUESTION_ID:
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == ""){
/**
* By default sort on student names
*/
sortOrder = KEY_ID;
}
Cursor c = qb.query(db, projection, selection, selectionArgs,
null, null, sortOrder);
/**
* register to watch a content URI for changes
*/
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case QUESTIONS:
count = db.delete(TABLE_QUESTION, selection, selectionArgs);
break;
case QUESTION_ID:
String id = uri.getPathSegments().get(1);
count = db.delete( TABLE_QUESTION, _ID + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" +
selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case QUESTIONS:
count = db.update(TABLE_QUESTION, values,
selection, selectionArgs);
break;
case QUESTION_ID:
count = db.update(TABLE_QUESTION, values, _ID +
" = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" +
selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri );
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
/**
* Get all student records
*/
case QUESTIONS:
return "com.android.cursor.dir/com.appex.questions";
/**
* Get a particular student
*/
case QUESTION_ID:
return "com.android.cursor.item/com.appex.questions";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment