1

我在应用程序中以编程方式设置通知铃声。

 @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)

怎么了 ?

4

1 回答 1

0

@user533844:查看第 552 行MainTabHost.onActivityResult。我认为它指的是这行代码:

RingtoneManager.setActualDefaultRingtoneUri(this, resultCode, uri);

您尝试传入resultCodewhich 不起作用,因为该方法setActualDefaultRingtoneUri要求type参数(即第二个参数)是TYPE_RINGTONETYPE_NOTIFICATIONTYPE_ALARM. 请参阅此处了解更多信息。

resultCode返回-1并且它仅用于处理返回的结果,就像你所拥有的一样if (resultCode == Activity.RESULT_OK && requestCode == 5)。它不应该在setActualDefaultRingtoneUri方法中使用。

于 2013-10-14T15:34:59.490 回答