亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

應用崩潰,但未捕獲任何異常

應用崩潰,但未捕獲任何異常

慕姐8265434 2022-09-01 16:34:03
因此,我正在Udemy的教程“完整的Android N開發人員課程”中學習,并試圖制作關于天氣應用程序的第86講。我從這里使用API https://openweathermap.org/current#cityid 并使用JSON來獲取所需的數據。當我輸入正確的城市名稱時,該應用程序工作正常,但是當輸入錯誤或清空時,應用程序崩潰而沒有捕獲任何異常。我不知道為什么它會崩潰,不知道在哪里看。所以我給你我寫的所有代碼。我試圖在這里和那里實現if語句,試圖找到它,但沒有任何運氣。我想知道問題出在哪里以及如何解決它,以便應用程序不再崩潰。提前致謝。public class MainActivity extends AppCompatActivity {    EditText editText;    String city = "";    TextView textView;    public void getWeather (View view)  {        try {            city = URLEncoder.encode(editText.getText().toString(), "UTF-8");            if (editText.getText().toString() == "") {                Toast.makeText(MainActivity.this, "Could not find weather", Toast.LENGTH_SHORT).show();                textView.setText("Please enter a city.");            } else  {                DownloadTask task = new DownloadTask();                task.execute("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=c6ef169a79d84674ef7e1414301eb5c4");            }            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);        } catch (UnsupportedEncodingException e1) {            Toast.makeText(MainActivity.this, "UnsupportedEncodingException", Toast.LENGTH_SHORT).show();        }catch (Exception e) {            Toast.makeText(MainActivity.this, "General exception (getWeather)", Toast.LENGTH_SHORT).show();        }    }    public class DownloadTask extends AsyncTask<String, Void, String> {        @Override        protected String doInBackground(String... urls) {            String result = "";            URL url;            HttpURLConnection urlConnection = null;            try {                url = new URL(urls[0]);                urlConnection = (HttpURLConnection)url.openConnection();                InputStream in = null;                in = urlConnection.getInputStream();                InputStreamReader reader = new InputStreamReader(in);               }
查看完整描述

1 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

這是因為您正在嘗試使用以下行在異步任務的 doInBackground(Params...) 方法內使用后臺線程更改 UI:

try {
     ...    return result;
} catch (MalformedURLException e1) {
    Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
} catch (IOException e2) {
Toast.makeText(MainActivity.this, "IOException", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
    Toast.makeText(MainActivity.this, "General exception (doInBackground)", Toast.LENGTH_SHORT).show();
}

你不應該在doInBackground(Params...)內調用Toast。在 onPostExecute(Result) 中執行此操作。

您可以通過忽略錯誤或在doInBackground中返回特定文本來避免這種情況。像這樣:

public class DownloadTask extends AsyncTask<String, Void, String> {


    @Override

    protected String doInBackground(String... urls) {


        String result = "";

        ...


        try {

            ...

            return result;

        } catch (MalformedURLException e1) {

            result= "MalformedURLException";

        } catch (IOException e2) {

            result= "IOException";

        } catch (Exception e) {

            // do nothing and returning empty

            result= "Exception";

        }

        return result;

    }


    @Override

    protected void onPostExecute(String result) {

        // check if there is an error

        String errorMessage = "";

        switch(result) {

          case "MalformedURLException":

            errorMessage = "MalformedURLException";

            break;

          case ""IOException":

            errorMessage = "IOException";

            break;

          case "Exception":

            errorMessage = "Exception";

            break;

        }


         // there is an error, show a message.

        if(!errorMessage.isEmpty()) {

            Toast.makeText(MainActivity.this, "Could not find weather: " + errorMessage, Toast.LENGTH_SHORT).show();

            return; // stop the process.

        }


         // do something when no error found.


    }

}


查看完整回答
反對 回復 2022-09-01
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號