我正在按照 Udemy 的教程制作天气应用程序。这也是我第一次使用 JSON 数据,所以我不确定这是否会影响我的情况。然而,每次我运行我的应用程序时,它都会打开;当我按下应该显示给定城市天气的按钮时,应用程序崩溃了。
\n\n我研究了 Looper.prepare() 因为它包含在错误中,但是添加循环程序不起作用(我是编码新手,我可能做得不正确)我也尝试添加 runOnUiThread (新的 Runnable) 无济于事(我可能又做错了)。
\n\n public class MainActivity extends AppCompatActivity {\n\n EditText editText;\n TextView resultTextView;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n editText = findViewById(R.id.editText);\n resultTextView = findViewById(R.id.resultTextView);\n\n }\n\n public void getWeather(View view) {\n try {\n DownloadTask task = new DownloadTask();\n String encodedCityName = \nURLEncoder.encode(editText.getText().toString(), "UTF-8");\n\n task.execute("http://openweathermap.org/data/2.5/weather?q=" \n+ encodedCityName + "&appid=b1b15e88fa797225412429c1c50c122a1");\n\n InputMethodManager mgr = (InputMethodManager) \ngetSystemService(Context.INPUT_METHOD_SERVICE);\n mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);\n } catch (Exception e) {\n e.printStackTrace();\n Toast.makeText(getApplicationContext(), "Could not find \nweather :(", …Run Code Online (Sandbox Code Playgroud)