2013-06-01 47 views
0

我正在处理将“手势”与活动(如海豚浏览器使用url)关联的应用程序。我想要做的是让用户选择与Action.PICK_ACTIVITY活动:当试图从Action.PICK_ACTIVITY返回的意图中启动活动时发生ActivityNotFoundException

Intent data = new Intent(Intent.ACTION_MAIN); 
     data.addCategory(Intent.CATEGORY_LAUNCHER); 
     Intent intent = new Intent(Intent.ACTION_PICK_ACTIVITY); 
     intent.putExtra(Intent.EXTRA_INTENT, data); 
     startActivityForResult(intent, PICK_ACTIVITY); 
... 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == PICK_ACTIVITY && resultCode == RESULT_OK) { 
     Gesture gesture = new Gesture(); 
     gesture.intent = data; 
     Intent intent = new Intent(this, GestureEditorActivity.class); 
       intent.putExtra("com.example.gesture.Gesture",gesture.toByteArray()); 
     startActivity(intent); 
    } 
} 

然后,当用户绘制相关的姿态,从我做起活动:

Intent intent = GestureList.getInstance(getApplicationContext()).getIntentForGesture(gesture);//intent corresponds to data in onActivityResult method 
    if(intent != null) 
     try { 
      startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    else 
     Toast.makeText(this, R.string.unrecognized_gesture, Toast.LENGTH_LONG).show(); 

但我得到了一个ActivityNotFoundException:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN dat=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;component=com.android.calculator2/.Calculator;end } 

这里是我的getIntentForGesture方法:

public Intent getIntentForGesture(Gesture gesture) 
{ 

    float bestDist = Float.MAX_VALUE; 
    Gesture bestGesture = null; 
    for(Gesture g:this) 
    { 
     float dist = gesture.distance(g); 
     if(dist < bestDist) 
     { 
      bestDist = dist; 
      bestGesture = g; 
     } 
    } 

    if(bestGesture != null) 
     return bestGesture.intent; 
    else 
     return null; 
} 

对不起,我蹩脚的英语,并在此先感谢您的回答

+0

它似乎问题在于你的清单xml。你有没有正确地添加清单中的活动? – stinepike

+0

是的,我做了,但我试图从另一个应用程序开始的活动 – andryr

+0

getIntentForGesture(手势);'?的代码在哪里?你的错误可能是该功能没有返回正确的意图。 – Femi

回答

0

你必须FLAG_ACTIVITY_NEW_TASK标志添加到您的意图

Intent intent = GestureList.getInstance(getApplicationContext()).getIntentForGesture(gesture);//intent corresponds to data in onActivityResult method 
if (intent != null) { 
    try { 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
    } catch (ActivityNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} else { 
    Toast.makeText(this, R.string.unrecognized_gesture, Toast.LENGTH_LONG).show(); 
} 
+0

最多据我所知,这种解决方案只有在他从一个活动外部调用'startactivity()'时才有效。他并没有真正告诉我们。如果它是一种服务,那么你是对的。 –

+0

我得到'ActivityNotFoundException不能解析为类型'。也许最好赶上'android.content.ActivityNotFoundException'。 –

+0

对不起,@ LuisA.Florit,你在哪里得到这个异常?已为'startActivity()'处理'ActivityNotFoundException' – ozbek

0

计算器活动不能被发现。也许在您的清单文件有问题,所以你可以尝试:

选项#1:

检查一切都写得很好。错误消息指出:

No Activity found to handle Intent { act=android.intent.action.MAIN dat=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;component=**com.android.calculator2/.Calculator**;end } 

斜杠/可能是错误。

选项#2:

你的情况的关键是,你试图打开从另一个包的活动(在这个特殊的姿态,计算器),所以你可以尝试检查,如果你可以启动计算器应用程序是这样的:

Intent calcInt = new Intent(); 
calcInt.setClassName("com.android.calculator2", "com.android.calculator2.Calculator"); 
startActivity(calcInt); 

如果计算器没有这种简单的代码推出,那么我敢打赌设备没有安装默认的计算器。

选项#3:

发表您的Android清单文件,以便我们可以检查它。

相关问题