我正在使用我的应用中的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)
String In = input.getText()。toString();
我认为你的问题就在上面一行..一个对象不能包含大写字母..所以,将(In)改为(in)..值得一试