在我的public void onActivityResult(int requestCode, int resultCode, Intent data) {
...有一个像这样的部分:
if (requestCode == 1) {
// Make sure the request was successful
final Uri videoUri = data.getData();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(getActivity(), videoUri);
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
timeInMillisec = Long.parseLong(time );
retriever.release();
在这里,我试图使用MediaMetadataRetriever
获取视频持续时间,并将其存储在timeInMillisec
中,接下来我正在检查timeInMillisec
的值是否有任何错误:
Toast.makeText(getActivity(), (int) timeInMillisec, Toast.LENGTH_SHORT).show();
关键是,这里我有这个例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zub.videoplay, PID: 9399
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.miui.gallery.open/raw//storage/emulated/0/Download/Air Supply - All Out Of Love.mp4 typ=video/mp4 flg=0x1 }} to activity {com.example.zub.videoplay/com.example.zub.videoplay.HomeActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
at android.app.ActivityThread.deliverResults(ActivityThread.java:4179)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4222)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1581)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
at android.content.res.Resources.getText(Resources.java:351)
at android.content.res.MiuiResources.getText(MiuiResources.java:97)
at android.widget.Toast.makeText(Toast.java:285)
at com.example.zub.videoplay.onActivityResult(PostFragment.java:293)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
at com.example.zub.videoplay.HomeActivity.onActivityResult(HomeActivity.java:586)
at android.app.Activity.dispatchActivityResult(Activity.java:7132)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4175)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4222)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1581)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
I/Process: Sending signal. PID: 9399 SIG: 9
我不知道这里有什么不对。任何帮助将不胜感激。
这是堆栈跟踪的重要部分:
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
at android.content.res.Resources.getText(Resources.java:351)
at android.content.res.MiuiResources.getText(MiuiResources.java:97)
at android.widget.Toast.makeText(Toast.java:285)
at com.example.zub.videoplay.onActivityResult(PostFragment.java:293)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
at com.example.zub.videoplay.HomeActivity.onActivityResult(HomeActivity.java:586)
它看起来像在HomeActivity.onActivityResult
,你试图通过Toast
显示一个不存在的字符串(ID#0x37c3f)。
问题是这一行
Toast.makeText(getActivity(), (int) timeInMillisec, Toast.LENGTH_SHORT).show();
它将调用toast类的makeText方法,因为没有id为timeInMillisec
的资源,因此该方法抛出Resources.NotFoundException
并使您的应用程序崩溃。
解决方案:将代码更改为
Toast.makeText(getActivity(), timeInMillisec + "", Toast.LENGTH_SHORT).show();
要么
Toast.makeText(getActivity(), String.valueOf(timeInMillisec), Toast.LENGTH_SHORT).show();