0

我正在開發一個Android應用程序,我通過Intents調用谷歌翻譯應用程序。描述的程序here真的很有幫助,但我無法弄清楚爲什麼我的應用程序崩潰,說「找不到處理意圖的活動」。通過Android的意圖訪問谷歌翻譯應用程序

在此先感謝!

這裏是我的代碼,

public void translate() 
{ 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, "hello"); 
    intent.putExtra("key_text_input", "hello"); 
    intent.putExtra("key_text_output", ""); 
    intent.putExtra("key_language_from", "en"); 
    intent.putExtra("key_language_to", "mal"); 
    intent.putExtra("key_suggest_translation", ""); 
    intent.putExtra("key_from_floating_window", false); 
      new ComponentName(
       "com.google.android.apps.translate", 
       "com.google.android.apps.translate.HomeActivity"); 
    startActivity(intent); 

} 

而這正是我的logcat顯示

04-20 07:29:01.647: E/AndroidRuntime(31465): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 (has clip) (has extras) } 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Activity.startActivityForResult(Activity.java:3388) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Activity.startActivityForResult(Activity.java:3349) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Activity.startActivity(Activity.java:3584) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.Activity.startActivity(Activity.java:3552) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at com.example.arch.vaani.ocr.FinalActivity.translate(FinalActivity.java:122) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at com.example.arch.vaani.ocr.FinalActivity$1.onClick(FinalActivity.java:101) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.view.View.performClick(View.java:4212) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.view.View$PerformClick.run(View.java:17476) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.os.Handler.handleCallback(Handler.java:800) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.os.Handler.dispatchMessage(Handler.java:100) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.os.Looper.loop(Looper.java:194) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at android.app.ActivityThread.main(ActivityThread.java:5371) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at java.lang.reflect.Method.invoke(Method.java:525) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-20 07:29:01.647: E/AndroidRuntime(31465): at dalvik.system.NativeStart.main(Native Method) 
+0

? –

+0

檢查您的設備是否安裝了Google翻譯? – TheLittleNaruto

+0

請按照此http://stackoverflow.com/a/20321335/1983018。有效。 – cuasodayleo

回答

0

我試着用谷歌翻譯應用程序的代碼,它不工作。 您可能必須使用付費服務Translate API

0

No Activity found to handle Intent意味着沒有Intent匹配Intent你開始沒有Google Translation App安裝在設備上。所以當你開始這個Intent這樣的場景時,你最好試試趕上ActivityNotFoundException。 代碼在這裏:

try { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, "hello"); 
    intent.putExtra("key_text_input", "hello"); 
    intent.putExtra("key_text_output", ""); 
    intent.putExtra("key_language_from", "en"); 
    intent.putExtra("key_language_to", "mal"); 
    intent.putExtra("key_suggest_translation", ""); 
    intent.putExtra("key_from_floating_window", false); 
    intent.setComponent(new ComponentName(
      "com.google.android.apps.translate", 
      "com.google.android.apps.translate.HomeActivity")); 
    startActivity(intent); 
} catch (ActivityNotFoundException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(getApplication(), "Sorry, No Google Translation Installed", 
       Toast.LENGTH_SHORT).show(); 
} 

最後,谷歌翻譯API可作爲一種有償服務。。檢查here

+0

即使安裝了應用程序,我也不認爲翻譯應用程序仍在工作。 –

+0

已嘗試'intent.setComponent(new ComponentName( 「com.google.android.apps.translate」, 「com.google.android.apps.translate.HomeActivity」));'?你甚至沒有在你的'Intent'上設置'ComponentName'。 – SilentKnight

+0

我也試過。和舊包名。不工作。你試過了嗎? –

2

你需要其中u呼籲`intent.setComponent`改變HomeActivityTranslateActivity

try { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, "hello"); 
    intent.putExtra("key_text_input", "hello"); 
    intent.putExtra("key_text_output", ""); 
    intent.putExtra("key_language_from", "en"); 
    intent.putExtra("key_language_to", "mal"); 
    intent.putExtra("key_suggest_translation", ""); 
    intent.putExtra("key_from_floating_window", false); 
    intent.setComponent(new ComponentName(
     "com.google.android.apps.translate", 
     //Change is here 
     //"com.google.android.apps.translate.HomeActivity")); 
     "com.google.android.apps.translate.TranslateActivity")); 
    startActivity(intent); 
} catch (ActivityNotFoundException e) { 
    // TODO Auto-generated catch block 
    Toast.makeText(getApplication(), "Sorry, No Google Translation Installed", 
      Toast.LENGTH_SHORT).show(); 
} 
+0

很好,它的工作原理 – user924