0

我在我的应用程序中使用Microsoft Translator API。我无法执行此代码:在Android中使用Microsoft Translator API

MainActivity:

public class MainActivity extends Activity implements OnClickListener { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    Translate.setClientId("My client"); 
    Translate.setClientSecret("secret key"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button Trans = (Button)findViewById(R.id.bTranslate); 
    Trans.setOnClickListener(this); 
} 

public void onClick(View v) { 
    //get the text entered 
    EditText input = (EditText)findViewById(R.id.etUserText); 
    TextView output = (EditText)findViewById(R.id.tvTranslatedText); 

    String In =input.getText().toString(); 
    //String Out; 
    try { 
     String Out = Translate.execute(In, Language.AUTO_DETECT, Language.FRENCH); 
     input.setText(Out); 
     output.setText(Out); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/etUserText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="31dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/tvTranslatedText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/etUserText" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="168dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/bTranslate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/etUserText" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp" 
     android:text="Translate" 
     android:onClick="trans"/> 



</RelativeLayout> 

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.translator" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" android:permission="android.permission.LOCATION_HARDWARE"> 
     <activity 
      android:name="com.example.translator.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

什么样的错误你得到 –

+0

而我安装“不幸的是,翻译已停止”E/AndroidRuntime(2011):java.lang.NoClassDefFoundError:com.memetix.mst.translate.Translate – kumar

+1

你有没有'Translate'类的jar文件?那个jar是在Android Dependencies部分添加的吗? –

回答

0

字符串= input.getText()的ToString();

我觉得你的问题是在上述..对象行不能包含大写字母..所以,变化(在)到(中)..值得尝试

相关问题