App closing when defining a Uri Android - java

When I execute this code it works fine in my emulator (even saves the image), but it closes the application on my phone (nexus 5)
When I set a try catch around the Uri line the program does not crash (but then again its null value could also have no influence on the code below it)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK) {
Bitmap x = (Bitmap) data.getExtras().get("data");
userImage.setImageBitmap(x);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "profileimage");
values.put(Images.Media.BUCKET_ID, "profileimage");
values.put(Images.Media.DESCRIPTION, "profile Image");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
outstream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
}
Cheers!
EDIT:
(sorry for my inactivity - new android phone that logs :D)
This is the error log:
03-28 15:33:51.230: E/Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/ext ActivityFilt[ 03-28 15:33:51.231 10005:10005 D/AndroidRuntime(10005): Shutting down VM
03-28 15:33:51.238: E/AndroidRuntime(10005): FATAL EXCEPTION: main
03-28 15:33:51.238: E/AndroidRuntime(10005): Process: com.td.tdabslidingmenu, PID: 10005
03-28 15:33:51.238: E/AndroidRuntime(10005): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.td.tdabslidingmenu/com.td.tdabslidingmenu.MainActivity}: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.-wrap16(ActivityThread.java)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.os.Looper.loop(Looper.java:148)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.main(ActivityThread.java:5417)
03-28 15:33:51.238: E/AndroidRuntime(10005): at java.lang.reflect.Method.invoke(Native Method)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-28 15:33:51.238: E/AndroidRuntime(10005): Caused by: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openOutputStream(ContentResolver.java:702)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.content.ContentResolver.openOutputStream(ContentResolver.java:678)
03-28 15:33:51.238: E/AndroidRuntime(10005): at com.td.tdabslidingmenu.MainActivity.onActivityResult(MainActivity.java:263)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.Activity.dispatchActivityResult(Activity.java:6456)
03-28 15:33:51.238: E/AndroidRuntime(10005): at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
03-28 15:33:51.238: E/AndroidRuntime(10005): ... 9 more

Which Android version is your Nexus 5 running? If it's Marshmallow (or the N preview), chances are it's a permission problem caused by the Media.EXTERNAL_CONTENT_URI part of your code.

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

Fragment cannot be cast to android.content.Context

I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);

Facebook Login Button crashes app when clicked

I am attempting to add a Facebook Login to my Android application. When I launch the application at first, it logs in without any issues, but afterwards, anytime I try to log in, the application crashes.
The MainActivity.java is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
List<String> permissionNeeds = Arrays.asList("public_profile", "user_friends");
loginButton.setReadPermissions(permissionNeeds);
try {
PackageInfo info = getPackageManager().getPackageInfo("com.example.ryans_000.facebooklogin", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
Log.v("LoginActivity", exception.getCause().toString()); }
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}}
The content_main contains the following piece of code for the button:
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
The Logcat is showing up the following:
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: FATAL EXCEPTION: main
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: Process: com.example.ryans_000.facebooklogin, PID: 23263
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.example.ryans_000.facebooklogin/com.example.ryans_000.facebooklogin.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.toString()' on a null object reference
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3988)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4038)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.access$1400(ActivityThread.java:150)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1443)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.os.Looper.loop(Looper.java:168)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5845)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.toString()' on a null object reference
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.example.ryans_000.facebooklogin.MainActivity$1.onError(MainActivity.java:92)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.facebook.login.LoginManager.finishLogin(LoginManager.java:579)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:216)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:159)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.example.ryans_000.facebooklogin.MainActivity.onActivityResult(MainActivity.java:100)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:6439)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3984)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4038) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.access$1400(ActivityThread.java:150) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1443) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.os.Looper.loop(Looper.java:168) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5845) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
02-29 09:34:45.306 23263-23263/com.example.ryans_000.facebooklogin E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
I think in your
#Override
public void onError(FacebookException exception) {
Log.v("LoginActivity", exception.getCause().toString()); }
exception.getCause is null hence trying to convert it to string is showing null pointer exception. Try fixing that.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.toString()' on a null object reference
Most probably,it points out to the line that has used toString() method which is in onError(), So try changing that.
Good Luck!

Clicking back button causes force close / NullPointerException

I'm experiencing a NullPointerException and ForceClose issue when clicking the back button after launching either the gallery or the default audio player using the android application I've built.
I've seen a few examples that state I'll need to finish the activity or check for a back button press - but I'm not sure how exactly that would fit into this scenario.
It's crashing on line 84 which is: Uri selectedImage = data.getData();
Source:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
{
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
}
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
LogCat:
04-24 21:04:26.441: D/AndroidRuntime(4491): Shutting down VM
04-24 21:04:26.441: W/dalvikvm(4491): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:26.451: E/AndroidRuntime(4491): FATAL EXCEPTION: main
04-24 21:04:26.451: E/AndroidRuntime(4491): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:26.451: E/AndroidRuntime(4491): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): Caused by: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:26.451: E/AndroidRuntime(4491): ... 11 more
04-24 21:04:28.721: I/Process(4491): Sending signal. PID: 4491 SIG: 9
04-24 21:04:39.681: D/AndroidRuntime(4543): Shutting down VM
04-24 21:04:39.681: W/dalvikvm(4543): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:39.691: E/AndroidRuntime(4543): FATAL EXCEPTION: main
04-24 21:04:39.691: E/AndroidRuntime(4543): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:39.691: E/AndroidRuntime(4543): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): Caused by: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.tablet.hairmax.UI.onActivityResult(UI.java:76)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:39.691: E/AndroidRuntime(4543): ... 11 more
04-24 21:05:14.191: D/AndroidRuntime(4586): Shutting down VM
04-24 21:05:14.191: W/dalvikvm(4586): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:05:14.191: E/AndroidRuntime(4586): FATAL EXCEPTION: main
04-24 21:05:14.191: E/AndroidRuntime(4586): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Looper.loop(Looper.java:137)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:05:14.191: E/AndroidRuntime(4586): at dalvik.system.NativeStart.main(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): Caused by: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:05:14.191: E/AndroidRuntime(4586): ... 11 more
data is null. You need to check before that line if data is null. Since you are pressing the Back button, you aren't sending an Intent back with setResult() like would normally be done.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (data != null)
{
if (requestCode == SELECT_VIDEO) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
}
See this answer about getting an image from gallery

How to get different file name when import from external memory to a listview

I use this script to insert a list of files stored in the external memory, inside a list view.
The script show the names of the files in alphabetical order and do not show the file extension.
ListView lv;
ArrayAdapter<MyFile> adapter;
public void list() {
lv = (ListView) findViewById(R.id.listView1);
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/folder/filesFolder/");
File[] filelist = dir.listFiles();
ArrayList<MyFile> theNamesOfFiles = new ArrayList<MyFile>();
for (File temp : filelist) {
theNamesOfFiles.add(new MyFile(temp, null));
}
Collections.sort(theNamesOfFiles, new Comparator<MyFile>() {
#Override
public int compare(MyFile lhs, MyFile rhs) {
return lhs.toString().compareToIgnoreCase(rhs.toString());
}
});
adapter = new ArrayAdapter<MyFile>(this, R.layout.list_row,theNamesOfFiles);
lv.setAdapter(adapter);
}
public class MyFile {
File file;
String fileName;
Activity activity;
public MyFile(File file,Activity activity)
{
this.activity = activity;
this.file = file;
this.fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
}
#Override
public String toString() {
return activity.getString(activity.getResources().getIdentifier(this.fileName, "string", getPackageName()));
}
}
so if i have this files in my internal memory:
file3.mp3
file1.mp3
file2.mp3
I will see this in my list view:
file1
file2
file3
But if i would like to show a different name in the listview from a string located in the string.xml, without change the name of the original files, how can i do it?
Example:
instead of have
file1
file2
file3
i would like to show:
black
orange
white
I have fixed the code you suggest, but now i get this error in the log cat if i try to run the app
03-28 12:00:13.048: E/AndroidRuntime(26058): FATAL EXCEPTION: main
03-28 12:00:13.048: E/AndroidRuntime(26058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.downloaddeletemaps/com.example.downloaddeletemaps.MyMapsActivity}: java.lang.NullPointerException
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.os.Looper.loop(Looper.java:137)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-28 12:00:13.048: E/AndroidRuntime(26058): at dalvik.system.NativeStart.main(Native Method)
03-28 12:00:13.048: E/AndroidRuntime(26058): Caused by: java.lang.NullPointerException
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.example.downloaddeletemaps.MyMapsActivity$MyFile.toString(MyMapsActivity.java:196)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.example.downloaddeletemaps.MyMapsActivity$1.compare(MyMapsActivity.java:53)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.example.downloaddeletemaps.MyMapsActivity$1.compare(MyMapsActivity.java:1)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.util.TimSort.sort(TimSort.java:185)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.util.TimSort.sort(TimSort.java:169)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.util.Arrays.sort(Arrays.java:2038)
03-28 12:00:13.048: E/AndroidRuntime(26058): at java.util.Collections.sort(Collections.java:1891)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.example.downloaddeletemaps.MyMapsActivity.list(MyMapsActivity.java:49)
03-28 12:00:13.048: E/AndroidRuntime(26058): at com.example.downloaddeletemaps.MyMapsActivity.onCreate(MyMapsActivity.java:78)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.Activity.performCreate(Activity.java:5283)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-28 12:00:13.048: E/AndroidRuntime(26058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-28 12:00:13.048: E/AndroidRuntime(26058): ... 11 more
I think i got error at this line
theNamesOfFiles.add(new MyFile(temp, null));
Seems that cannot find resource String
03-28 13:04:24.415: E/AndroidRuntime(2063): FATAL EXCEPTION: main
03-28 13:04:24.415: E/AndroidRuntime(2063): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.downloaddeletemaps/com.example.downloaddeletemaps.MyMapsActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.os.Looper.loop(Looper.java:137)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-28 13:04:24.415: E/AndroidRuntime(2063): at dalvik.system.NativeStart.main(Native Method)
03-28 13:04:24.415: E/AndroidRuntime(2063): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.content.res.Resources.getText(Resources.java:1057)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.content.res.Resources.getString(Resources.java:1149)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.content.Context.getString(Context.java:332)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.example.downloaddeletemaps.MyMapsActivity$MyFile.toString(MyMapsActivity.java:196)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.example.downloaddeletemaps.MyMapsActivity$1.compare(MyMapsActivity.java:53)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.example.downloaddeletemaps.MyMapsActivity$1.compare(MyMapsActivity.java:1)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.util.TimSort.sort(TimSort.java:185)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.util.TimSort.sort(TimSort.java:169)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.util.Arrays.sort(Arrays.java:2038)
03-28 13:04:24.415: E/AndroidRuntime(2063): at java.util.Collections.sort(Collections.java:1891)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.example.downloaddeletemaps.MyMapsActivity.list(MyMapsActivity.java:49)
03-28 13:04:24.415: E/AndroidRuntime(2063): at com.example.downloaddeletemaps.MyMapsActivity.onCreate(MyMapsActivity.java:78)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.Activity.performCreate(Activity.java:5283)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-28 13:04:24.415: E/AndroidRuntime(2063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-28 13:04:24.415: E/AndroidRuntime(2063): ... 11 more
Try this:
In Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="file1">black</string>
<string name="file2">orange</string>
<string name="file3">white</string>
</resources>
In Activity class:
ListView lv;
ArrayAdapter<MyFile> adapter;
public void list() {
lv = (ListView) findViewById(R.id.listView1);
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/folder/filesFolder/");
File[] filelist = dir.listFiles();
ArrayList<MyFile> theNamesOfFiles = new ArrayList<MyFile>();
for (File temp : filelist) {
theNamesOfFiles.add(new MyFile(temp, this));
}
Collections.sort(theNamesOfFiles, new Comparator<MyFile>() {
#Override
public int compare(MyFile lhs, MyFile rhs) {
return lhs.toString().compareToIgnoreCase(rhs.toString());
}
});
adapter = new ArrayAdapter<MyFile>(this, R.layout.list_row,theNamesOfFiles);
lv.setAdapter(adapter);
}
public class MyFile {
File file;
String fileName;
Activity activity;
public MyFile(File file,Activity activity)
{
this.activity = activity;
this.file = file;
this.fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
}
#Override
public String toString() {
return String.valueOf(activity.getString(activity.getResources().getIdentifier(this.fileName, "string", activity.getPackageName())));
}
}
Regards

Categories