我想在我的 android 项目上使用谷歌翻译 API。以下是我的代码。它作为java项目工作,但是当我在android设备上尝试时,项目停止了。是什么原因?我添加了 googletranslateapi-1.0.jar,
翻译器.java
package com.example.calendar;
import com.gtranslate.Language;
import com.gtranslate.Translator;
public class translator {
public String translate(String text) {
Translator translate = Translator.getInstance();
String text2 = translate.translate(text, Language.TURKISH,
Language.ENGLISH);
return text2;
}
}
在主要活动中
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == VR_REQUEST && resultCode == RESULT_OK) {
ArrayList<String> suggestedWords = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Context context = getApplicationContext();
text = suggestedWords.get(0);
int duration = Toast.LENGTH_SHORT;
ClassificationActivity classificationActivity = new ClassificationActivity();
String category = classificationActivity.control(text);
translator trans = new translator();
String ingtext = trans.translate(text);
CalendarEvent cv = new CalendarEvent();
Date date = new Date();
ContentResolver cr = ctx.getContentResolver();
Uri uri = cr.insert(Events.CONTENT_URI, CalendarEvent
.toICSContentValues(cv, date.getTime(), category));
Toast toast = Toast.makeText(context, ingtext, duration);
toast.show();
}