我正在应用程序中以编程方式设置Notification铃声。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Log.i("Speak", "I picked this ringtone " + uri);
if (uri != null)
{
RingtoneManager.setActualDefaultRingtoneUri(this, resultCode, uri);
Settings.System.putString(getContentResolver(), Settings.System.NOTIFICATION_SOUND, uri.toString());
notification_uri = uri.toString();
}
}
}
它在Android 4.0.4上运行良好。它在Android 4.1.2上崩溃了应用程序
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=-1, data=Intent { (has extras) }} to activity {com.aa.me.vianet85/com.aa.me.vianet.activity.MainTabHost}: java.lang.NullPointerException: key == null
at android.app.ActivityThread.deliverResults(ActivityThread.java:3208)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3251)
at android.app.ActivityThread.access$1200(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4962)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: key == null
at android.os.Parcel.readException(Parcel.java:1431)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
at android.content.ContentResolver.insert(ContentResolver.java:866)
at android.provider.Settings$NameValueTable.putString(Settings.java:683)
at android.provider.Settings$System.putString(Settings.java:945)
at android.media.RingtoneManager.setActualDefaultRingtoneUri(RingtoneManager.java:825)
at com.aa.me.vianet.activity.MainTabHost.onActivityResult(MainTabHost.java:552)
at android.app.Activity.dispatchActivityResult(Activity.java:5344)
at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:126)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3204)
... 11 more
java.lang.NullPointerException: key == null
at android.os.Parcel.readException(Parcel.java:1431)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
at android.content.ContentResolver.insert(ContentResolver.java:866)
at android.provider.Settings$NameValueTable.putString(Settings.java:683)
at android.provider.Settings$System.putString(Settings.java:945)
at android.media.RingtoneManager.setActualDefaultRingtoneUri(RingtoneManager.java:825)
at com.aa.me.vianet.activity.MainTabHost.onActivityResult(MainTabHost.java:552)
at android.app.Activity.dispatchActivityResult(Activity.java:5344)
at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:126)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3204)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3251)
at android.app.ActivityThread.access$1200(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4962)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
怎么了?
答案 0 :(得分:0)
@ user533844:查看MainTabHost.onActivityResult
的第552行。我认为它引用了这行代码:
RingtoneManager.setActualDefaultRingtoneUri(this, resultCode, uri);
您尝试传入的resultCode
无效,因为方法setActualDefaultRingtoneUri
要求type
参数(即第二个参数)为TYPE_RINGTONE
之一,{ {1}}或TYPE_NOTIFICATION
。有关详细信息,请参阅here。
TYPE_ALARM
返回 -1 ,它仅用于处理返回的结果,就像您拥有的resultCode
一样。它不应该用在if (resultCode == Activity.RESULT_OK && requestCode == 5)
方法中。