I have coded an application that contains an list that present contact information.
Application code is as follow:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor mCursor=getContacts();
startManagingCursor(mCursor);
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.two_line_list_item,
mCursor, new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME},
new int[]{android.R.id.text1, android.R.id.text2});
setListAdapter(adapter);
}
private Cursor getContacts(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "='" + ("1") + "'";
String[] selectionArgs = null;
String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor out=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
return out;
}
}
but application was crashed at managedQuery line.
I add also READ_CONTACTS permission to the manifest file.
What is wrong?
Edited:
Here is logcat data:
--------- beginning of crash
08-18 17:36:45.093 6464-6464/com.example.morteza.listactivitycursor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.morteza.listactivitycursor, PID: 6464
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.morteza.listactivitycursor/com.example.morteza.listactivitycursor.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{afac265 6464:com.example.morteza.listactivitycursor/u0a98} (pid=6464, uid=10098) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{afac265 6464:com.example.morteza.listactivitycursor/u0a98} (pid=6464, uid=10098) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4181)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5970)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2592)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1828)
at android.content.ContentResolver.query(ContentResolver.java:786)
at android.content.ContentResolver.query(ContentResolver.java:752)
at android.content.ContentResolver.query(ContentResolver.java:710)
at com.example.morteza.listactivitycursor.MainActivity.getContacts(MainActivity.java:35)
at com.example.morteza.listactivitycursor.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12185)
at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12582)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:357)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3288)
at android.os.Binder.execTransact(Binder.java:731)
08-18 17:36:45.112 6464-6464/com.example.morteza.listactivitycursor I/Process: Sending signal. PID: 6464 SIG: 9
What is wrong?
You must get the user's permission. The manifest is not enough.
Check this: https://developer.android.com/training/permissions/requesting
Change to this:
private Cursor getContacts(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "= ?";
String[] selectionArgs = new String[] { "1" };
String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor out=getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
return out;
}
Related
intent = getIntent();
final String userid = intent.getStringExtra("userid");
I have Checked and verified that user id is coming but can not use in this child but before 2-3 days the same code working for me, from to now i have not changed anything
I have userid from Adapter position and i can use it in this activity and its working but in the given line of code error occur
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
that can not pass java.lang.NullPointerException:
Can't pass null for argument 'pathString' in child()
Error is given as below:
2020-02-27 21:46:53.567 3248-3248/com.example.chat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chat, PID: 3248
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chat/com.example.chat.MessageActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database##19.2.0:96)
at com.example.chat.MessageActivity.onCreate(MessageActivity.java:175)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Code part is given as below
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
Last_seen.setText(user.getStatus());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
So I initially only wanted to access contact names, and I only declared my permission in the manifest. My code was working fine then. But now I'm trying out accessing the contact number too, and that's giving me the following in logcat:
2019-11-05 18:56:02.738 30808-30919/? E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:705)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:565)
at android.content.ContentProvider$Transport.query(ContentProvider.java:224)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:752)
2019-11-05 18:54:09.212 5113-5113/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fresh, PID: 5113
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2644i1d5681210d9c25a6.1450iSkype_108/738 flg=0x1 }} to activity {com.example.android.fresh/com.example.android.fresh.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5113, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:4506)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5113, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:432)
at android.content.ContentResolver.query(ContentResolver.java:847)
at android.content.ContentResolver.query(ContentResolver.java:762)
at android.content.ContentResolver.query(ContentResolver.java:720)
at com.example.android.fresh.MainActivity.getContactNumber(MainActivity.java:163)
at com.example.android.fresh.MainActivity.onActivityResult(MainActivity.java:129)
at android.app.Activity.dispatchActivityResult(Activity.java:7476)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4499)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-11-05 18:56:02.740 5361-5361/com.example.android.fresh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fresh, PID: 5361
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2644i179d0ece8ca0b7f9.3789r1113-2916221214.1450iSkype_1113.1419r4429-2916221214.847r4510-2916221214/4513 flg=0x1 }} to activity {com.example.android.fresh/com.example.android.fresh.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.app.ActivityThread.deliverResults(ActivityThread.java:4506)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=5361, uid=10178 requires android.permission.READ_CONTACTS, or grantUriPermission()
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:432)
at android.content.ContentResolver.query(ContentResolver.java:847)
at android.content.ContentResolver.query(ContentResolver.java:762)
at android.content.ContentResolver.query(ContentResolver.java:720)
at com.example.android.fresh.MainActivity.getContactNumber(MainActivity.java:163)
at com.example.android.fresh.MainActivity.onActivityResult(MainActivity.java:129)
at android.app.Activity.dispatchActivityResult(Activity.java:7476)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4499)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4548)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is the relevant code within getContactName()
Cursor cur = getContentResolver().query(uriContact, null, null, null, null);
if (cur.moveToFirst()) {
contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));}
And the relevant code within getContactNumber()
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
returnval = true;
}
cursorPhone.close();
How do I fix this?
use this
public class MainActivity extends AppCompatActivity {
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Read and show the contacts
showContacts();
}
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
getContactNames();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
showContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Read the name of all the contacts.
*
* #return a list of names.
*/
private void getContactNames() {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
if(people.moveToFirst()) {
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// add number to list
// Do work...
} while (people.moveToNext());
}
people.close();
}
}
I'm having problems while attempting to save data input (from the user) via two EditText and transferring it to another activity where it should be displayed as a ViewText.
Code from activity/data input
public class DatosBasicos extends AppCompatActivity {
EditText presion;
EditText ritmo;
Button atriaje;
String valorritmo;
String valorpresion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_basicos);
atriaje = (Button) findViewById(R.id.triaj);
presion = (EditText) findViewById(R.id.presion);
ritmo = (EditText) findViewById(R.id.ritmo);
atriaje.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
valorritmo = ritmo.getText().toString();
valorpresion = presion.getText().toString();
Intent i = new Intent(DatosBasicos.this, Triaje.class);
i.putExtra("PRESION",valorpresion);
i.putExtra("RITMO",valorritmo);
startActivity(i);
finish();
}
});
}
}
Code from activity/data output
public class Triaje extends AppCompatActivity {
TextView rit, pres;
String ritS, presS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triaje);
rit = (TextView) findViewById(R.id.ritmoval);
pres = (TextView) findViewById(R.id.presionval);
ritS = getIntent().getExtras().getString("PRESION");
presS = getIntent().getExtras().getString("RITMO");
rit.setText(ritS);
pres.setText(presS);
}
}
ERROR LOG
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplicaciontriaje, PID: 2569
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplicaciontriaje/com.example.aplicaciontriaje.Triaje}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at com.example.aplicaciontriaje.Triaje.onCreate(Triaje.java:19)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Disconnected from the target VM, address: 'localhost:8619', transport: 'socket'
WHAT SHOULD THE APP DO
I expect to get my data input into my Triaje activity and displayed in the two corresponding ViewText.
I get the following error: "App keeps stopping"
Is it possible to debug the system ? Then you clearly understand exactly where the it gives you error! Then tell exactly in which line it gives error!
Trying to add a song to the MediaPlayer without R.raw.audiofilename. Making a music player and I obviously don't have the audio file being packaged with the app.
More detail: I am creating buttons to add to a scroll view, and on click, play the song.
The on click method seems to be where the error happening.
private void addSongs() {
Toast.makeText(this, "Searching", Toast.LENGTH_SHORT).show();
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(BASE_URI, BASE_PROJECTION, BASE_SELECTION, null, BASE_SORTORDER);
int count;
if (cursor != null) {
count = cursor.getCount();
if (count > 0) while (cursor.moveToNext()) createButton(cursor);
}
}
private void createButton(final Cursor cursor) {
final String TITLE = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
final String PATH = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
Button button = new Button(this);
button.setText(TITLE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mediaPlayer = MediaPlayer.create(
MainActivity.this, Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "Music/" + TITLE + ".m4a")
);
mediaPlayer.start();
}
});
songList.addView(button);
}
This is pretty much all the code so far in the program, I am worried that my logic is the error here.
Exception thrown:
D/MediaPlayer: create failed:
java.io.IOException: setDataSource failed.
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1168)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1141)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1059)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1001)
at android.media.MediaPlayer.create(MediaPlayer.java:908)
at android.media.MediaPlayer.create(MediaPlayer.java:885)
at android.media.MediaPlayer.create(MediaPlayer.java:864)
at theredspy15.fosyplayer.MainActivity$1.onClick(MainActivity.java:80)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: theredspy15.fosyplayer, PID: 24561
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at theredspy15.fosyplayer.MainActivity$1.onClick(MainActivity.java:84)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
EDIT: I am using the .m4a as the audio files I am using to test the app in the emulator are .m4a. Will change that later
This code used to work fine with the Nexus 5 image gallery. Ever since moving to the newer Google Photos app selecting images crash this code.
Error:
08-07 10:10:10.792 25086-25086/com.app.app E/CursorWindow﹕ Failed to
read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns.
08-07 10:10:10.795 25086-25086/com.app.app D/AndroidRuntime﹕ Shutting
down VM 08-07 10:10:10.801 25086-25086/com.app.app E/AndroidRuntime﹕
FATAL EXCEPTION: main
Process: com.app.app, PID: 25086
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent {
dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/17870/ACTUAL/1285012603
flg=0x1 (has clip) }} to activity
{com.app.app/com.app.app.PhotoActivity}:
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.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at com.app.app.PhotoActivity.getRealPathFromURI(PhotoActivity.java:456)
at com.app.app.PhotoActivity.onActivityResult(PhotoActivity.java:436)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
ONACTIVITYRESULT:
case ACTION_SELECT_PHOTO: {
if (resultCode == RESULT_OK) {
Uri selectedImageURI = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageURI)); // LINE 436
mCurrentPhotoPath = imageFile.toString();
if (mCurrentPhotoPath != null) {
setPic();
mCurrentPhotoPath = null;
}
}
break;
}
GETREALPATHFROMURI:
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx); // HERE IS THE ERROR: LINE 456
cursor.close();
}
return result;
}
I'd guess my issue is in one of two places, either getContentResolver().query needs to use some newer method. OR the getColumnIndex(MediaStore.Images.ImageColumns.DATA) MediaStore is the incorrect way to deal with the newer cloud based photo apps.
A URI does not mean there is a file. An app is able to provide the contents through any mechanism it wants (a local file, a local file in a private directory you can't read, streamed over the internet, built dynamically on the fly).
Instead, you should use ContentResolver.openInputStream(contentUri) to get the contents from a URI. You can then transform that into a Bitmap via BitmapFactory.decodeStream()