当我尝试用户选择菜单项时,我试图从我的应用程序中调用Android默认SMS应用程序的删除消息活动。我的Android版本是4.2.2。
我在logcat中收到以下错误:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException<br/>
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DELETE dat=sms:
以下是代码:
public class SmsWiperActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_wiper);
TextView wItem = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("item");
// displaying selected product name
wItem.setText(product);
}
public void invokeMessagingApplication(MenuItem item)
{
/*Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);*/
Intent smsIntent = new Intent(Intent.ACTION_DELETE);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);
/*Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_APP_MESSAGING);
startActivity(smsIntent);*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sms_wiper, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
//settings();
return true;
case R.id.action_MessagingApplication:
invokeMessagingApplication(item);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Logcat文件是
08-16 13:30:39.277: E/AndroidRuntime(29487): FATAL EXCEPTION: main
08-16 13:30:39.277: E/AndroidRuntime(29487): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.view.MenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(MenuInflater.java:235)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:144)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:178)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.widget.AbsListView.performItemClick(AbsListView.java:1507)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3292)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.widget.AbsListView$1.run(AbsListView.java:4545)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.os.Handler.handleCallback(Handler.java:725)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.os.Handler.dispatchMessage(Handler.java:92) 08-16 13:30:39.277: E/AndroidRuntime(29487): at android.os.Looper.loop(Looper.java:176)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.ActivityThread.main(ActivityThread.java:5317)
08-16 13:30:39.277: E/AndroidRuntime(29487): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 13:30:39.277: E/AndroidRuntime(29487): at java.lang.reflect.Method.invoke(Method.java:511)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-16 13:30:39.277: E/AndroidRuntime(29487): at dalvik.system.NativeStart.main(Native Method)
08-16 13:30:39.277: E/AndroidRuntime(29487): Caused by: java.lang.reflect.InvocationTargetException
08-16 13:30:39.277: E/AndroidRuntime(29487): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 13:30:39.277: E/AndroidRuntime(29487): at java.lang.reflect.Method.invoke(Method.java:511)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.view.MenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(MenuInflater.java:231)
08-16 13:30:39.277: E/AndroidRuntime(29487): ... 16 more
08-16 13:30:39.277: E/AndroidRuntime(29487): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DELETE dat=sms: }
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1659)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Activity.startActivityForResult(Activity.java:3434)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Activity.startActivityForResult(Activity.java:3395)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Activity.startActivity(Activity.java:3630)
08-16 13:30:39.277: E/AndroidRuntime(29487): at android.app.Activity.startActivity(Activity.java:3598)
08-16 13:30:39.277: E/AndroidRuntime(29487): at com.rishi.android.contactswiper.SmsWiperActivity.invokeMessagingApplication(SmsWiperActivity.java:42)
08-16 13:30:39.277: E/AndroidRuntime(29487): ... 19 more
答案 0 :(得分:1)
错误日志似乎很清楚。您的设备上没有可以处理此类意图的活动 您确定此API是公开的吗?
顺便说一句,你应该总是检查一个Activity是否可以处理一个Intent以避免像这样崩溃: public static boolean isIntentAvailable(Context context, Intent intent) {
return !context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).isEmpty();
}