应用崩溃,但未捕获任何异常

因此,我正在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);

       

        }


慕姐8265434
浏览 86回答 1
1回答

白衣染霜花

这是因为您正在尝试使用以下行在异步任务的&nbsp;doInBackground(Params...)&nbsp;方法内使用后台线程更改 UI:try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;result; }&nbsp;catch&nbsp;(MalformedURLException&nbsp;e1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Toast.makeText(MainActivity.this,&nbsp;"MalformedURLException",&nbsp;Toast.LENGTH_SHORT).show(); }&nbsp;catch&nbsp;(IOException&nbsp;e2)&nbsp;{ Toast.makeText(MainActivity.this,&nbsp;"IOException",&nbsp;Toast.LENGTH_SHORT).show(); }&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Toast.makeText(MainActivity.this,&nbsp;"General&nbsp;exception&nbsp;(doInBackground)",&nbsp;Toast.LENGTH_SHORT).show(); }你不应该在doInBackground(Params...)内调用Toast。在&nbsp;onPostExecute(Result) 中执行此操作。您可以通过忽略错误或在doInBackground中返回特定文本来避免这种情况。像这样:public class DownloadTask extends AsyncTask<String, Void, String> {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String doInBackground(String... urls) {&nbsp; &nbsp; &nbsp; &nbsp; String result = "";&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; } catch (MalformedURLException e1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result= "MalformedURLException";&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result= "IOException";&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do nothing and returning empty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result= "Exception";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(String result) {&nbsp; &nbsp; &nbsp; &nbsp; // check if there is an error&nbsp; &nbsp; &nbsp; &nbsp; String errorMessage = "";&nbsp; &nbsp; &nbsp; &nbsp; switch(result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "MalformedURLException":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorMessage = "MalformedURLException";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ""IOException":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorMessage = "IOException";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "Exception":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorMessage = "Exception";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// there is an error, show a message.&nbsp; &nbsp; &nbsp; &nbsp; if(!errorMessage.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "Could not find weather: " + errorMessage, Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; // stop the process.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// do something when no error found.&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java