强制关闭铃声选择器
Posted
技术标签:
【中文标题】强制关闭铃声选择器【英文标题】:Force Close on RingtonePicker 【发布时间】:2012-06-01 08:18:38 【问题描述】:我的代码拒绝返回正确的 requestCode,所以我即兴创作了一点。它总是返回 -1。
if(v.getId() == R.id.imageButton9)
request = 888;
Intent wpIntent = new Intent();
wpIntent.setType("image/*");
wpIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"), SELECT_PICTURE);
else if(v.getId() == R.id.imageButton10)
String uri = null;
request = 999;
rtIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
if( uri != null)
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(uri));
else
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
this.startActivityForResult(rtIntent, SELECT_TONE);
我在代码开头声明了两个变量:
protected static final int SELECT_PICTURE = 888;
protected static final int SELECT_TONE = 999;
这是 onActivityResult:
public void onActivityResult(int resultCode, int requestCode, Intent data)
Toast.makeText(this, "onActivityResult: "+requestCode, Toast.LENGTH_SHORT).show();
if(request == 888)
Toast.makeText(this, "Picture Selection", Toast.LENGTH_SHORT).show();
Uri selectedImage = Uri.parse(data.getDataString());
wpPath = getPath(selectedImage);
Toast.makeText(this, "WP Path: "+wpPath, Toast.LENGTH_SHORT).show();
request = 0;
else if(request == 999)
Toast.makeText(this, "Tone Selection", Toast.LENGTH_SHORT).show();
Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());
Uri uri = rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
rtPath = uri.toString();
Toast.makeText(this, "Ringtone Path: "+rtPath, Toast.LENGTH_SHORT).show();
request = 0;
这总是强制关闭。它给出以下错误:
06-01 12:40:35.388: E/androidRuntime(7437): java.lang.RuntimeException: 传递结果失败 ResultInfowho=null, request=999, result=-1, data=Intent (有额外的) 到活动 com.asim.autobot/com.asim.autobot.profile: java.lang.NullPointerException
我被困住了。无法解决这个问题。
首先,为什么requestCode总是返回-1? 其次,为什么应用强制关闭rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);?
编辑: 完整的Logcat http://i47.tinypic.com/1zm2gww.jpg
它指向的 Line 是 Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());
【问题讨论】:
使用 switch case 而不是用 if (if(v.getId() == R.id.imageButton9)) 来检查按钮 【参考方案1】:当您获得-1
的结果代码时,它的工作方式为RESULT_OK = -1
检查以下行:
rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString()
您有一个名为 Intent data
的参数,您应该使用它而不是 rtIntent
变量。您的 rtIntent 变量不会被修改,因此 getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
可能会返回 null。
【讨论】:
我爱你,男人,以一种非常非同性恋的方式。非常感谢您的帮助。我已将您的答案标记为已接受。请再次检查这个问题,因为我可能需要一些帮助来将返回的 Uri 设置为铃声。希望我可以在 *** 之外与您联系以寻求帮助:/ 搜索也有效 :) 试试这个解决方案:***.com/questions/7671637/… 我看到了...看到我的代码还不错。只是我用错了意图哈哈【参考方案2】:试试这个代码,
public void onClick(View v)
switch (v.getId())
case R.id.imageButton9:
request = 888;
Intent wpIntent = new Intent();
wpIntent.setType("image/*");
wpIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"), SELECT_PICTURE);
case R.id.imageButton10:
String uri = null;
request = 999;
rtIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
if( uri != null)
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(uri));
else
rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
this.startActivityForResult(rtIntent, SELECT_TONE);
break;
default:
break;
public void onActivityResult(int resultCode, int requestCode, Intent data)
Toast.makeText(this, "onActivityResult: "+requestCode, Toast.LENGTH_SHORT).show();
if(requestCode == SELECT_PICTURE)
Toast.makeText(this, "Picture Selection", Toast.LENGTH_SHORT).show();
Uri selectedImage = Uri.parse(data.getDataString());
wpPath = getPath(selectedImage);
Toast.makeText(this, "WP Path: "+wpPath, Toast.LENGTH_SHORT).show();
request = 0;
else if(requestCode == SELECT_TONE)
Toast.makeText(this, "Tone Selection", Toast.LENGTH_SHORT).show();
Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());
Uri uri = rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
rtPath = uri.toString();
Toast.makeText(this, "Ringtone Path: "+rtPath, Toast.LENGTH_SHORT).show();
request = 0;
【讨论】:
试过了。不工作。当我单击选择铃声时,activityresult 返回 -1,因此什么也没有。当我点击选择壁纸时,它首先会打开 RingtoneChooser(无论是使用 Android 系统还是 GoSMS)。当我按回时,它会打开 WallpaperChooser(图库、根资源管理器等)。以上是关于强制关闭铃声选择器的主要内容,如果未能解决你的问题,请参考以下文章