android.content.res.Resources $ NotFoundException:资源ID#0x88a6fd

时间:2016-01-29 23:56:10

标签: java android android-contentprovider simplecursoradapter

我是初学者,我试图通过单击片段中的按钮来加载来自数据库的联系人,然后将传出呼叫也保存在数据库中? 的 ContactsFragemt.java

public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>
{

SimpleCursorAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.contact, container, false);
    final Button Button = (Button) view.findViewById(R.id.load_button);
    final SQLDataBaseAdapter sqlDataBaseHelper = new SQLDataBaseAdapter(getActivity());


    //*************************** method for population main contacts listView *********************************//

    Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            while (c.moveToNext()) {
                String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String image_uri = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));


                int position = 0;
                int exist = 0;
                boolean imageComp, nameComp;
                String[] contactsData = sqlDataBaseHelper.getContacts(position);
                while (exist == 0 && (contactsData[0] != (null) || contactsData[1] != (null) || contactsData[2] != (null))) // we are checking that if it is reached at the end or not
                {
                    if (contactsData[1] != null) {
                        if (contactsData[1].equals(phNumber))  // will make update if we got matched with phone number and if any of ther other parameter is changed
                        {
                            if (contactsData[2] == null) {
                                // pic is null saved in data base and
                                if (image_uri != null) {
                                    sqlDataBaseHelper.updateTable1(null, null, null, null, contactsData[2], image_uri);
                                    // then update new pic here
                                }
                            } else // but if their is pic
                                if (!contactsData[2].equals(image_uri)) { // and he/she update pic with a brand new picture then
                                    sqlDataBaseHelper.updateTable1(null, null, null, null, contactsData[2], image_uri);
                                }
                            if (contactsData[0] == null) {
                                // name is null saved in data base and
                                if (contactName != null) {
                                    sqlDataBaseHelper.updateTable1(contactsData[0], contactName, null, null, null, null);
                                    // then update new name here
                                }
                            } else  // if name was saved previously in based
                                if (!contactsData[0].equals(contactName)) {   //but the guy changed his name so
                                    sqlDataBaseHelper.updateTable1(contactsData[0], contactName, null, null, null, null);
                                }
                            exist = 1;
                        }
                    }
                    position++;
                    contactsData = sqlDataBaseHelper.getContacts(position);
                }
                if (exist == 0)  // means if number is not in the list then make update
                {

                    long id = sqlDataBaseHelper.insertData1(contactName, phNumber, image_uri);
                    if (id < 0) {
                        Toast.makeText(getActivity(), "Data1 Insertion is unsuccessful", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "Data1 Insertion is successful", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            String[] fromFieldNames = sqlDataBaseHelper.fromFieldName1();
            int[] toViewIDs = sqlDataBaseHelper.toIds1();
            adapter = new SimpleCursorAdapter(getActivity(),
                    R.layout.list_items_view,
                    null,
                    fromFieldNames,
                    toViewIDs,0);
            ListView mainList = (ListView) view.findViewById(R.id.main_list_view);
            mainList.setAdapter(adapter);
            getLoaderManager().initLoader(0, null, ContactsFragment.this);

        }

    });
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Uri uri = ContentProvider.CONTENT_URI;
    return new CursorLoader(getActivity(), uri, null, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader loader) {
    adapter.swapCursor(null);
}



}

SimpleCursorAdapter.java

public class SimpleCursorAdapter extends android.widget.SimpleCursorAdapter {

Context mcontext;
String[] values;
Cursor cursor;
int[] to;

public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.values = from;
    this.to = to;
    this.cursor = c;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(list_items_view, parent, false);
    final ImageButton call = (ImageButton) view.findViewById(R.id.call);
    ImageButton sms = (ImageButton) view.findViewById(R.id.sms);
    final TextView contact_no = (TextView) view.findViewById(R.id.contact_no);
    final TextView contactName = (TextView) view.findViewById(R.id.contact_name);
    ImageView contactImage = (ImageView) view.findViewById(R.id.contact_image);

    final SQLDataBaseAdapter sqlDataBaseHelper = new SQLDataBaseAdapter(mcontext);


    //********************************** method for calling from app****************************/

    call.setOnClickListener(new View.OnClickListener() {    //when phone button is clicked
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(String.valueOf("tel:" + contact_no.getText().toString())));
            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//*/
            if (ActivityCompat.checkSelfPermission(mcontext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mcontext.startActivity(callIntent);//*/

            Calendar rightNow = Calendar.getInstance();
            int hourOfDay = rightNow.get(Calendar.HOUR_OF_DAY);
            String AM_PM;

            if (hourOfDay > 12) {   // for 12 hours format
                hourOfDay = hourOfDay - 12;
                AM_PM = "PM";
            } else {
                AM_PM = "AM";
            }

            String time = Integer.toString(hourOfDay)
                    + " : " + Integer.toString(rightNow.get(Calendar.MINUTE))
                    + " " + AM_PM;

            String date = Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH))
                    + "/" + Integer.toString(rightNow.get(Calendar.MONTH) + 1)
                    + "/" + Integer.toString(rightNow.get(Calendar.YEAR));

            String name = contactName.getText().toString();
            Cursor c = mcontext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);

            if (c.moveToNext() == true) {
                String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
                String pic = Uri.parse("android.resource://com.example.asim.simpleviewpager/drawable/outgoing_call.png").toString();
                long id = sqlDataBaseHelper.insertData2(name, duration, pic, time, date);
                if (id < 0) {
                    Toast.makeText(mcontext, "Data2 Insertion is unsuccessful", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mcontext, "Data2 Insertion is successful", Toast.LENGTH_SHORT).show();
                }
            }
            c.close();
        }
    });

    //********************************** method for sending message from app****************************//

    sms.setOnClickListener(new View.OnClickListener() {     //when sms button is clicked
        @Override
        public void onClick(View v) {
            Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + contact_no.getText().toString()));
            smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mcontext.startActivity(smsIntent);
            Calendar rightNow = Calendar.getInstance();
            int hourOfDay = rightNow.get(Calendar.HOUR_OF_DAY);
            String AM_PM;

            if (hourOfDay > 12) {
                hourOfDay = hourOfDay - 12;
                AM_PM = "PM";
            } else {
                AM_PM = "AM";
            }

            String time = Integer.toString(hourOfDay)
                    + " : " + Integer.toString(rightNow.get(Calendar.MINUTE))
                    + " " + AM_PM;

            String date = Integer.toString(rightNow.get(Calendar.DAY_OF_MONTH))
                    + "/" + Integer.toString(rightNow.get(Calendar.MONTH) + 1)
                    + "/" + Integer.toString(rightNow.get(Calendar.YEAR));

            String name = contactName.getText().toString();
            String duration = "000 000 000";
            String pic = Uri.parse("android.resource://com.example.asim.simpleviewpager/drawable/message_sent.png").toString();
            long id = sqlDataBaseHelper.insertData2(name, duration, pic, time, date);
            if (id < 0) {
                Toast.makeText(mcontext, "Data2 Insertion is unsuccessful", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mcontext, "Data2 Insertion is successful", Toast.LENGTH_SHORT).show();
            }
        }

    });
    //************************** Reading contact from SQLDataBase for each item*************************************//

     String[] contactsData = sqlDataBaseHelper.getContacts(position);
    if (contactsData[0] != (null)) {

        contactName.setText(contactsData[0]);
        contact_no.setText(contactsData[1]);
        if (contactsData[2] == (null)) {
            contactImage.setImageResource(R.drawable.w4j8n);
        } else {
            contactImage.setImageURI(Uri.parse(contactsData[2]));
        }
    }//*/

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

}
}

ContentProvider.java

public class ContentProvider extends android.content.ContentProvider {

public static final String PROVIDER_NAME = "com.example.asim.simpleviewpager";  //.contentprovider
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/ContactsDataBase");
private static final int CONTENTPROVIDERS = 1;
private static final UriMatcher uriMatcher ;
static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, "ContactsDataBase", CONTENTPROVIDERS);
}

SQLDataBaseAdapter sqlDataBaseAdapter;
@Override
public boolean onCreate() {
    sqlDataBaseAdapter = new SQLDataBaseAdapter(getContext());
    return true;
}

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    if (uriMatcher.match(uri) == CONTENTPROVIDERS) {
        return sqlDataBaseAdapter.getAllContacts();
    } else {
        return null;
    }
}

@Nullable
@Override
public String getType(Uri uri) {
    return null;
}

@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    return 0;
}
}

SQLDataBaseAdapter.java

public class SQLDataBaseAdapter {

SQLDataBaseHelper sqlDataBaseHelper;

public SQLDataBaseAdapter(Context context){
    sqlDataBaseHelper = new SQLDataBaseHelper(context);
}
public long insertData1(String contactName, String contactNo, String pic){

    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    ContentValues contentValues =  new ContentValues();
    contentValues.put(SQLDataBaseHelper.NAME, contactName);
    contentValues.put(SQLDataBaseHelper.NO, contactNo);
    contentValues.put(SQLDataBaseHelper.PICTURE, pic);
    long id = db.insert(SQLDataBaseHelper.TABLE1_NAME,null,contentValues);
    return id;
}
public long insertData2(String contactName, String duration, String time, String date, String pic ){

    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    ContentValues contentValues =  new ContentValues();
    contentValues.put(SQLDataBaseHelper.NAME, contactName);
    contentValues.put(SQLDataBaseHelper.DURATION, duration);
    contentValues.put(SQLDataBaseHelper.TIME, time);
    contentValues.put(SQLDataBaseHelper.DATE, date);
    contentValues.put(SQLDataBaseHelper.PICTURE, pic);
    long id = db.insert(SQLDataBaseHelper.TABLE2_NAME,null,contentValues);
    return id;
}
public String[] getContacts(int position) {
    String[] contactsData = new String[3];
    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    String[] columns = {SQLDataBaseHelper.UID, SQLDataBaseHelper.NAME, SQLDataBaseHelper.PICTURE, SQLDataBaseHelper.NO};
    Cursor cursor = db.query(SQLDataBaseHelper.TABLE1_NAME, columns, null, null, null, null, null);
    cursor.moveToPosition(position);
    int pos = cursor.getPosition();
    int cnt = cursor.getCount();
    int checkElement = cnt-pos;
    if (checkElement > 0)  
    {
        cursor.moveToPosition(position); 
        int nameColumnIndex = cursor.getColumnIndex(SQLDataBaseHelper.NAME);
        String name = cursor.getString(nameColumnIndex);
        int noColumnIndex = cursor.getColumnIndex(SQLDataBaseHelper.NO);
        String contactNo = cursor.getString(noColumnIndex);
        int picColumnIndex = cursor.getColumnIndex(SQLDataBaseHelper.PICTURE);
        String picture = cursor.getString(picColumnIndex);
        contactsData[0] = name;
        contactsData[1] = contactNo;
        contactsData[2] = picture;
    } else   
    {
        contactsData[0] = null;
        contactsData[1] = null;
        contactsData[2] = null;
    }
    cursor.close();
    db.close();
    return contactsData;
}
public void updateTable1 (String oldName, String newName, String oldPhoneNo, String NewPhoneNumber, String oldPic, String newPic) {
    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    if (oldName != newName) { 
        ContentValues contentValues = new ContentValues();
        contentValues.put(SQLDataBaseHelper.NAME, newName);
        String[] whereArgs = {oldName};
        db.update(SQLDataBaseHelper.TABLE1_NAME,contentValues,SQLDataBaseHelper.NAME+" =? ",whereArgs);
    }
    if(oldPhoneNo!=NewPhoneNumber){
        ContentValues contentValues = new ContentValues();
        contentValues.put(SQLDataBaseHelper.NO, NewPhoneNumber);
        String[] whereArgs = {oldPhoneNo};
        db.update(SQLDataBaseHelper.TABLE1_NAME,contentValues,SQLDataBaseHelper.NO+" =? ",whereArgs);
    }
    if(oldPic!=newPic){
        ContentValues contentValues = new ContentValues();
        contentValues.put(SQLDataBaseHelper.PICTURE, NewPhoneNumber);
        String[] whereArgs = {oldPic};
        db.update(SQLDataBaseHelper.TABLE1_NAME,contentValues,SQLDataBaseHelper.PICTURE+" =? ",whereArgs);
    }

}
public void deleteRowTable1()
{

}
public void updateTable2 ()
{

}
public void deleteRowTable2()
{

}


/////////////////////////////////////////////////////////////////////
public Cursor getAllContacts(){
    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    return db.query(SQLDataBaseHelper.TABLE1_NAME, new String[] {
                    SQLDataBaseHelper.UID,SQLDataBaseHelper.NAME, SQLDataBaseHelper.NO, SQLDataBaseHelper.PICTURE},
            null, null, null, null,
            SQLDataBaseHelper.NAME + " asc ");
}


public SQLDataBaseAdapter open() {
    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    return this;
}
public Cursor getAllRows1() {
    SQLiteDatabase db = sqlDataBaseHelper.getWritableDatabase();
    Cursor cursor = db.query(true, SQLDataBaseHelper.TABLE1_NAME, SQLDataBaseHelper.ALL_KEYS1, null, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}


public String[] fromFieldName1(){///
    String[] fields = new String[] {SQLDataBaseHelper.UID,SQLDataBaseHelper.NAME, SQLDataBaseHelper.NO, SQLDataBaseHelper.PICTURE};
    return fields;
}
public int[] toIds1(){
    int[] toViewIds = new int[]{R.id.contact_name,R.id.contact_no,R.id.contact_image};
    return toViewIds;
}



static class SQLDataBaseHelper extends SQLiteOpenHelper{

     private static final String DATABASE_NAME = "ContactsDataBase";
     private static final String TABLE1_NAME = "CALLS_TABLE1";
     private static final String TABLE2_NAME = "LOGS_TABLE";
     private static final int DATABASE_VERSION = 1;
     private static final String UID = "_id";
     private static final String NAME = "Name";
     private static final String NO = "ContactNo";
     private static final String DURATION = "Duration";
     private static final String PICTURE = "Picture";
     private static final String DATE = "Date";
     private static final String TIME = "Time";
     private static final String[] ALL_KEYS1 = new String[] {UID,NAME, NO, PICTURE};

     private static final String CREATE_TABLE1 =  "CREATE TABLE "+TABLE1_NAME+" (" +UID+
             " INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+NO+" VARCHAR(255), "
             +PICTURE+" VARCHAR(255));";

     private static final String CREATE_TABLE2 =  "CREATE TABLE "+TABLE2_NAME+" ("
             +UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+DURATION+" VARCHAR(255), "
             +PICTURE+" VARCHAR(255), " +DATE+ " VARCHAR(255), "+TIME+ " VARCHAR(255));";

     private static final String DROP_TABLE1 = "DROP TABLE  IF EXIST"+ TABLE1_NAME ;
     private static final String DROP_TABLE2 = "DROP TABLE  IF EXIST"+ TABLE2_NAME ;
     private Context context;


    public SQLDataBaseHelper (Context context) {           
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            db.execSQL(CREATE_TABLE1);
            Toast.makeText(context,"onCreate1 called" , Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            Toast.makeText(context,""+e , Toast.LENGTH_SHORT).show();
            Log.e("exception in onCreate", "here is exception  " + e);
        } //*/
        try {
            db.execSQL(CREATE_TABLE2);
            Toast.makeText(context,"onCreate2 called" , Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            Toast.makeText(context,""+e , Toast.LENGTH_SHORT).show();
            Log.e("exception in onCreate", "here is exception  " + e);
        } //*/
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            db.execSQL(DROP_TABLE1);
            Toast.makeText(context,"onUpgrade1 called" , Toast.LENGTH_SHORT).show();
            onCreate(db);
        }catch (SQLException e){
            Toast.makeText(context, ""+e, Toast.LENGTH_SHORT).show();
        }

        try {
            db.execSQL(DROP_TABLE2);
            Toast.makeText(context,"onUpgrade2 called" , Toast.LENGTH_SHORT).show();
            onCreate(db);
        }catch (SQLException e){
            Toast.makeText(context, ""+e, Toast.LENGTH_SHORT).show();
        }
    }
}

}

这些是错误

01-30 04:03:23.804 2702-2702/com.example.asim.simpleviewpager E/AndroidRuntime: FATAL EXCEPTION: main      
android.content.res.Resources$NotFoundException: Resource ID #0x88a6fd
at android.content.res.Resources.getValue(Resources.java:1049)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at android.support.v4.widget.SimpleCursorAdapter.setViewImage(SimpleCursorAdapter.java:195)
at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:143)

1 个答案:

答案 0 :(得分:1)

在SimpleCursorAdapter.java中,您有一个方法bindView(),它从其父类重写相同的方法,但您不在该方法中放置任何代码。删除方法应修复错误。但是如果你打算覆盖该方法中绑定的工作原理,你可能想要从那里开始一些代码,比如

super.bindView(view, context, cursor);