Android 铃声选择器 Activity 在某些设备上崩溃......特别是 Nexus S

发布于 2024-11-28 06:43:52 字数 3957 浏览 2 评论 0原文

我的应用程序中有以下代码,允许用户选择当我的应用程序通知用户时将播放的特定铃声(我不想更改设备上的默认铃声):

public void ringtonePicker() {

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);

    this.startActivityForResult(intent, 999);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode != RESULT_OK) {

    return;
    }

    if (requestCode == 999) {

        Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

        if (uri != null) 
        {

            String ringTonePath = uri.toString();
            SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens shared preference editor
            editor.putString("storedRingtoneLocation", ringTonePath);
            editor.commit();


            populateList(settingsList, settingsListDetails = getAndSetDetails(settingsList.length));
            adapter.notifyDataSetChanged(); 
        }
        else 
        {
            Toast.makeText(getApplicationContext(), "No Ringtone Path Found!", Toast.LENGTH_LONG).show();
        }
    }

    else 
    {
        Toast.makeText(getApplicationContext(), "Request Code is Bad", Toast.LENGTH_LONG).show();
    }

    super.onActivityResult(requestCode, resultCode, intent);
}

这在我的测试设备(我只有一台......HTC Evo)和运行 2.2 和 2.3 的模拟器中有效。

事实上,我知道这会导致 Nexus S 强制关闭,正如我的用户报告的那样。我不知道此代码遇到问题的其他设备的型号名称。有什么想法吗?开发人员控制台的堆栈跟踪显示了这一点:

java.lang.RuntimeException:无法启动活动 ComponentInfo{android/com.android.internal.app.RingtonePickerActivity}:java.lang.NullPointerException 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 在 android.app.ActivityThread.access 2300 美元(ActivityThread.java:125) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:123) 在 android.app.ActivityThread.main(ActivityThread.java:4627) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 在dalvik.system.NativeStart.main(本机方法) 引起原因:java.lang.NullPointerException 在 com.android.internal.app.RingtonePickerActivity.onPrepareListView(RingtonePickerActivity.java:214) 在 com.android.internal.app.AlertController$AlertParams.createListView(AlertController.java:905) 在 com.android.internal.app.AlertController$AlertParams.apply(AlertController.java:824) 在 com.android.internal.app.AlertActivity.setupAlert(AlertActivity.java:73) 在 com.android.internal.app.RingtonePickerActivity.onCreate(RingtonePickerActivity.java:188) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 更多

具体来说,您可以在 RingtonePickerActivity.java:214 中看到 nullPointerException。检查 RingtonePickerActivity.java 在 grepcode 并查看类,可以在第 214 行看到该类正在寻找整数addDefaultRintoneItem

我不知道有哪个拥有 Nexus S 的人可以测试这个,但我想删除这条线 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true 从我的代码中可能会纠正空指针。

有人能验证我的理论吗?没有其他人遇到过这个问题吗?我只在 SO 上找到了一个与此问题相关的线程,并且该线程活动似乎在得出答案之前就已终止。没有其他人经历过这种情况还是我只是以错误的方式这样做?

I have the following code in my app to allow the user to select a specific ringtone which will be played when my app notifies the user (I'm not trying to change the default ringer on the device):

public void ringtonePicker() {

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);

    this.startActivityForResult(intent, 999);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode != RESULT_OK) {

    return;
    }

    if (requestCode == 999) {

        Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

        if (uri != null) 
        {

            String ringTonePath = uri.toString();
            SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens shared preference editor
            editor.putString("storedRingtoneLocation", ringTonePath);
            editor.commit();


            populateList(settingsList, settingsListDetails = getAndSetDetails(settingsList.length));
            adapter.notifyDataSetChanged(); 
        }
        else 
        {
            Toast.makeText(getApplicationContext(), "No Ringtone Path Found!", Toast.LENGTH_LONG).show();
        }
    }

    else 
    {
        Toast.makeText(getApplicationContext(), "Request Code is Bad", Toast.LENGTH_LONG).show();
    }

    super.onActivityResult(requestCode, resultCode, intent);
}

This is working on my test device (I only have one....HTC Evo) and in my emulators running 2.2 and 2.3.

I know for fact that this will cause a force-close on the Nexus S as my users have reported. I do not know the model names of other devices with which this code experiences issues. Any ideas? The stack trace from the developer console shows this:

java.lang.RuntimeException: Unable to start activity ComponentInfo{android/com.android.internal.app.RingtonePickerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.internal.app.RingtonePickerActivity.onPrepareListView(RingtonePickerActivity.java:214)
at com.android.internal.app.AlertController$AlertParams.createListView(AlertController.java:905)
at com.android.internal.app.AlertController$AlertParams.apply(AlertController.java:824)
at com.android.internal.app.AlertActivity.setupAlert(AlertActivity.java:73)
at com.android.internal.app.RingtonePickerActivity.onCreate(RingtonePickerActivity.java:188)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

Specifically, you can see a nullPointerException at RingtonePickerActivity.java:214. After inspect RingtonePickerActivity.java at grepcode and looking at class, one can see at line 214 that the class is looking for the integer addDefaultRintoneItem.

I don't know of anyone with a Nexus S could test this out, but I was thinking that removing the line
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true
from my code might correct the nullPointer.

Can anyone validate my theory? Has no one else experienced this issue? I've only found one thread on SO pertaining to this issue and it seems the thread activity died before an answer was derived. Has no one else experienced this or am I just doing this the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2024-12-05 06:43:52

无论出于何种原因,铃声选择器有时不存在。您已经获得了一个空指针,只需将其包装在 try 缓存中并显示一个对话框,说明铃声选择器不存在。

Ringtone picker does not exist sometimes for whatever reason. You've gotten a null pointer, just wrap it in a try cache and show a dialog saying the ringtone picker doesn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文