Yorumlar
Parametlerin null gidip gitmediğini kontrol eder misiniz. Eğer edittext varsa listener eklemek gerekebilir. Eklemediğin için de boş string gönderdiğinizden bu hatayı veriyor olabilir.
Furkan
5 yıl 1 ay önce
Nereyi değiştirmem gerekiyor.Rica etsem siz kontrol edebilir misiniz?Teşekkürler :)
ImageView iv_tr,iv_en;
Button btn_changelan,btn_translate;
EditText translatearea;
TextView showarea;
String languages ="tr-en";
String searchedword;
String yandexkey ="(yandexapikey)";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
iv_tr =(ImageView)findViewById(R.id.iv_tr);
iv_tr.setTag(R.drawable.language1);
iv_en =(ImageView)findViewById(R.id.iv_en);
iv_en.setTag(R.drawable.language2);
btn_changelan =(Button)findViewById(R.id.btn_changelan);
btn_translate =(Button)findViewById(R.id.btn_translate);
translatearea =(EditText)findViewById(R.id.et_TranslateArea);
translatearea.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
showarea =(TextView)findViewById(R.id.tv_ShowArea);
btn_changelan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
languages = change(iv_tr,iv_en);
}
});
btn_translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchedword =translatearea.getText().toString();
String query = null;
try {
query = URLEncoder.encode(searchedword, "utf-8");
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
});
}
public String change(ImageView flag1,ImageView flag2){
String languages = null;
int flag1Tag=(Integer)flag1.getTag();
int flag2Tag =(Integer)flag2.getTag();
flag1.setImageResource(flag2Tag);
flag1.setTag(flag2Tag);
flag2.setImageResource(flag1Tag);
flag2.setImageResource(flag1Tag);
if ((Integer)flag1.getTag() == R.drawable.language1 && (Integer)flag2.getTag() == R.drawable.language2){
languages="tr-e+n";
}
else if ((Integer)flag1.getTag() == R.drawable.language2 && (Integer)flag2.getTag() == R.drawable.language1){
languages="en-tr";
}
return languages;
}
class TranslatorBackgroundTask extends AsyncTask{
@Override
protected String doInBackground(String... params){
String urlString=params[0];
StringBuilder jsonString = new StringBuilder();
try {
URL yandexUrl = new URL(urlString);
HttpURLConnection httpURLConnection =(HttpURLConnection)yandexUrl.openConnection();
InputStream inputStream =httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line=bufferedReader.readLine()) != null ){
jsonString.append(line);
}
inputStream.close();
bufferedReader.close();
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return jsonString.toString();
}
@Override
protected void onPostExecute(String json){
JsonObject jsonObject =new JsonParser().parse(json).getAsJsonObject();
String result =jsonObject.get("text").getAsString();
showarea.setText(result);
}
}
}