I made a list to display all the images I captured with their details. When I choose a list item opens a new screen with delete and edit option. When i choose to delete the item my app crashes.
In the logcat displays thiw message:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.os.Bundle.getString(java.lang.String)' on a
null object reference
and points this code :
ImageView iv_photo = (ImageView) findViewById(R.id.cloth_image);
iv_photo.setImageBitmap(BitmapFactory.decodeFile(extras.getString("photograph")));
In the database the delete method is :
public Boolean deleteCloth(int id) {
SQLiteDatabase db = this.getWritableDatabase();
int i = db.delete(SQLITE_TABLE, KEY_ROWID + " = ?", new String[]{String.valueOf(id)});
db.close();
if (i != 0) {
return true;
} else {
return false;
}
}
I don't know what to do to fix it! Any help?
It's because the "extras" bundle is null. Investigate why is it so. Maybe your have not initialised it.
Related
I'm trying to create an application to display the call logs based on the call type (incoming calls, outgoing calls, or missing calls). In addition I'm trying to add search and delete functionalities so the user can search for the call (by number) and delete the call. The layout of the app is shown here:
The search functionality works if I hit the 'All' button to display all calls and search by number there, but upon going to other sections to search for a number, such as received or missed, the app crashes.
So far I have managed to run the logcat on Android Studio, and found that the main issue is in my main activity file. I have attached the logcat image here:
There seems to be issues with these pieces of code: my getCalls Method, afterTextChanged Method, and my updateCursor Method.
private Cursor getCalls(int type, String searchString) {
Uri callUri = CallLog.Calls.CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
String[] projection = new String[]{Calls._ID, Calls.NUMBER, Calls.DURATION, Calls.TYPE};
String selection=null;
String[] selectionArgs=null;
if(type != 0){
// filter type calls
selection = Calls.TYPE + "=?";
selectionArgs = new String[]{String.valueOf(type)};
}
if(!TextUtils.isEmpty(searchString)) {
// has some search string
if(TextUtils.isEmpty(selection)) {
// all call types
selection = Calls.NUMBER + " LIKE ?";
selectionArgs = new String[]{"%"+searchString+"%"};
} else {
// some type of call and add search String
selection = selection+" && " + Calls.NUMBER+" LIKE ?";
selectionArgs = new String[]{selectionArgs[0],"'%"+searchString+"%'"};
}
}
String order = Calls.DATE + " DESC ";
//verify permissions to access the user's call log
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
cursor = contentResolver.query(callUri, // URI content provider
projection,
selection,
selectionArgs,
order);
}
return cursor;
}
#Override
public void afterTextChanged(Editable s) {
updateCursor();
}
//updates the search
void updateCursor() {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
cursor = null;
}
cursor = getCalls(currentCallType, searchET.getText().toString());
adapter.swapCursor(cursor);
}
Upon running the app, I expected to be able to go to different sections (for example, I go to the received call section) and search for a number, however upon going to the section and tapping on the search bar, the app crashes. I do not understand how there could be issues with these methods.
SQLException says the error is near the & symbol, so the error should be in this line:
selection = selection+" && " + Calls.NUMBER+" LIKE ?";
Let's replace it with this one, because 'AND' should be used in SQL instead of double &:
selection = selection+" AND " + Calls.NUMBER+" LIKE ?";
before voting down please read my question , which I have searched a lot but I couldn't find the answer yet, so I would appreciate if you give me hand to overcome the problem.
Actually I need to update a tuple in a table named "Demographics". But it seems my code does not work correctly, and in fact after running the app , I got the result "0" for updating which means nothing get updated.
12-21 12:34:54.190 2351-2367/? D/Update Result:: =0
I guess my problem is due to not pointing to the right row of the table based on Primary key. Actually when a user Register to my app the following things should happen:
1- Create a tuple in "Demographics" table --> username, password and email will be inserted. An auto increment primary key also constructed and inserted.
2- user logins , then he can complete rest of information in "Demographics" table. --> this MODIFICATION is the "update" process which I', asking.
Would you please tell me if the following codes are wrong or have any implicit error?
DemographicsCRUD.java
public long UpdateDemographics(Demographics_to demoId) {
//SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DataBaseHelper.lastName, demoId.getD_lastName());
values.put(DataBaseHelper.firstName, demoId.getD_firstName());
values.put(DataBaseHelper.dateOfBirth, demoId.getD_dateOfBirth())
long result = database.update(dbHelper.Demographics_Table, values,
WHERE_ID_EQUALS,
new String[]{String.valueOf(demoId.getD_patientID())});
Log.d("Update Result:", "=" + result);
// db.close();
return result;
}
here is where I call the above code:
private void updateDemographicsTable()
{
ep_demoId = new Demographics_to();
String ep_na = ep_name.getText().toString();
String ep_fa = ep_family.getText().toString();
.
.
.
ep_demoId.setD_dateOfBirth(ep_bd);
ep_demoId.setD_firstName(ep_na);
ep_demoId.setD_lastName(ep_fa);
}
#Override
protected Long doInBackground(Void... arg0) {
long result = ep_demoCRUD.UpdateDemographics(ep_demoId);
return result;
}
#Override
protected void onPostExecute(Long result) {
if (activityWeakRef.get() != null
&& !activityWeakRef.get().isFinishing()) {
if (result != -1)
Toast.makeText(activityWeakRef.get(), "Information Updated!",
Toast.LENGTH_LONG).show();
}}
Looks like whatever you are passing in as the patientID does not have a matching record in the database or the dataobject "Demographics_to" has the patient ID set incorrectly.
I'm trying to grab album information from MediaStore. I can get the name and artist very easily but for some reason the ALBUM_ID column is giving me an error when I try to access it.
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:450)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51 )
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at sage.musicplayer.MainActivity.getAlbumList(MainActivity.java:1540)
at sage.musicplayer.MainActivity.onCreate(MainActivity.java:238)
I can't seem to find a solution. Any help is appreciated! Below is the method I have to grab the album information and add them to an ArrayList.
public ArrayList<Album> getAlbumList() {
ArrayList<Album> temp = new ArrayList<>();
/*String[] proj = {MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.NUMBER_OF_SONGS
};*/
Cursor albumCursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null, null);
if(albumCursor != null && albumCursor.moveToFirst()) {
int albumName = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int albumArtist = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
do {
String thisAlbumName = albumCursor.getString(albumName);
String thisAlbumArtist = albumCursor.getString(albumArtist);
String thisAlbumID = albumCursor.getString(albumID);//this line is giving me an error
temp.add(new Album(thisAlbumName, thisAlbumArtist, thisAlbumID));
}while(albumCursor.moveToNext());
}
return temp;
}
I too faced this issue. You can easily solve this issue by changing
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
TO
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums._ID);
That is you need to use _ID whenever you want AlbumID of the Album. I have no idea why MediaStore developers used _ID instead of ALBUM_ID, etc stuff.
in my app searchbar is there, wheren in user types the text. Whenever a text get changes in the filed i will call a query to DB to get the related search items. But sometimes it crashes.
Here is the code i'm doing to call DB
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
if(newText.trim().equals(""))
{
return false;
}
//showSearchSuggestions(newText);
mfilterdata = mController.get_controllerObj().getDBManager().getAllSuggestedFilter(newText);
if(mSearchadapter != null)
mSearchadapter.swapCursor(mfilterdata);
return false;
}
Here is how i m querying in DB manager
public Cursor getAllSuggestedFilter(String filterString)
{
String READ_QUERY = "SELECT * FROM " + Tbl_ITEM_TABLE + " where "+
item.TITLE + " Like" + "\"%" + filterString + "%"+"\"";
if(mcursorForFilter != null)
{
mcursorForFilter.close();
mcursorForFilter = null;
}
try
{
mcursorForFilter = getReadableDatabase().rawQuery(READ_QUERY, null);
}
catch(Exception ee)
{
}
return mcursorForFilter;
}
randomly i get exception like
java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM itemtable where title Like"%t%")
That's probably because you are closing the cursor in the wrong place, and trying to use it after that, but you cannot use an already closed cursor.
I would get rid of this part of the code:
if(mcursorForFilter != null) {
mcursorForFilter.close();
mcursorForFilter = null;
}
Instead, close the old cursor after you set the new one. swapCursor() returns the old Cursor, or returns null if there was not a cursor set, also returns null if the if you try to swap the same instance of the previously set cursor. Knowing that, you can try something like this:
Cursor c = mSearchadapter.swapCursor(mfilterdata);
if(c != null)
c.close();
Try that, and let me know if that helped.
Note that when you are using a Loader (LoaderManager.LoaderCallbacks), the framework is going to close the old cursor. This is what the documentation says:
onLoadFinished:
The loader will release the data once it knows the application is no
longer using it. For example, if the data is a cursor from a
CursorLoader, you should not call close() on it yourself. ...
I'm developing app to provide and set pictures for contacts from social networks, and I'm already figure out how to set picture to contact, but when I do this programmatically picture is stored in lower quality.
For example here is 2 examples, first picture is set with my app, second with native android contacts app (source picture is exactly the same for both cases):
First example
With my app:
With native app:
Second example
With my app:
With native app:
You can see pixilization on the hands and other parts.
The code I've used to set contact picture:
public static boolean setContactPhoto(long contactId, byte[] photo) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
long photoId = -1;
long rawContactId = -1;
Cursor rawContactsCursor = cr.query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID},
String.format("%s=%d", ContactsContract.RawContacts.CONTACT_ID, contactId),
null,
null
);
while (rawContactsCursor.moveToNext()) {
rawContactId = rawContactsCursor.getLong(rawContactsCursor.getColumnIndex(ContactsContract.RawContacts._ID));
String where = String.format(
"%s=%d AND %s=='%s'",
ContactsContract.Data.RAW_CONTACT_ID,
rawContactId,
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
);
Cursor dataCursor = cr.query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data._ID},
where,
null,
null
);
if (dataCursor.moveToFirst()) {
photoId = dataCursor.getLong(dataCursor.getColumnIndex(ContactsContract.Data._ID));
dataCursor.close();
break;
}
dataCursor.close();
}
rawContactsCursor.close();
if (rawContactId < 0) return false;
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoId < 0) {
return cr.insert(ContactsContract.Data.CONTENT_URI, values) != null;
} else {
return cr.update(ContactsContract.Data.CONTENT_URI, values, String.format("%s=%d", ContactsContract.Data._ID, photoId), null) == 1;
}
}
My phone has 540x960 resolution, so source pictures are 960x960, this is strange, but if I try to set 540x960 picture it is cropped from all sides and scaled up, so pixilization is bigger.
So, how to avoid such pixilization setting up contact picture programmatically?
As described in the documentation, the .photo field hold the "Thumbnail photo of the raw contact."
I think you have to set the PHOTO_FILE_ID used to "accessing full-size photos by photo file ID".
See the related ContactsContract.DisplayPhoto