进入 Toast 后,我​​的天气应用程序崩溃了

my weather app getting crashing after i enter the Toast

我的应用在此代码中正常运行,所有城市天气数据均正确显示。如果有人输入错误的城市名称,我想祝酒词

我能理解错误是什么android工作室没有给出任何错误。如果输入城市名称可以正常工作,但如果我输入错误的城市名称或任何其他词,它就会崩溃

工作代码:::

package com.study.whatstheweather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    EditText editText;
TextView textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
textView2 = findViewById(R.id.textView2);
    }

    public void getweather (View view){
        Downlordtask task = new Downlordtask();
        task.execute("https://openweathermap.org/data/2.5/weather?q="+ editText.getText().toString()+ "&appid=439d4b804bc8187953eb36d2a8c26a02");
        InputMethodManager methodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        methodManager.hideSoftInputFromWindow(editText.getWindowToken(),0);

    }


    public class Downlordtask 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 inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                int data = inputStreamReader.read();

                while (data !=-1){ char curretnt = (char) data; result += curretnt; data = inputStreamReader.read(); }  return  result;}
            catch (Exception e){e.printStackTrace(); 
            return null; }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);


            try {
                JSONObject jsonObject = new JSONObject(s);
                String wetherinfo = jsonObject.getString("weather");
                Log.i("weather",wetherinfo);
                JSONArray array = new JSONArray(wetherinfo);

                String message="";

                for (int i=0; i <array.length();i++){
                    JSONObject jsonPart = array.getJSONObject(i);

                    String main = jsonPart.getString("main");
                    String discrip = jsonPart.getString("description");

                    if (!main.equals("") && !discrip.equals("")){message += main + ":" + discrip + "\r\n";
                    }
                }

                    if(!message.equals("")) { textView2.setText(message);  }
            } catch (Exception e){e.printStackTrace();}
        }
    }

} 


然后我在这个文件中输入了 Toast,现在应用程序崩溃了

崩溃代码....


public class MainActivity extends AppCompatActivity {

    EditText editText;
TextView textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
textView2 = findViewById(R.id.textView2);
    }

    public void getweather (View view){
        Downlordtask task = new Downlordtask();
        task.execute("https://openweathermap.org/data/2.5/weather?q="+ editText.getText().toString()+ "&appid=439d4b804bc8187953eb36d2a8c26a02");
        InputMethodManager methodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        methodManager.hideSoftInputFromWindow(editText.getWindowToken(),0);

    }


    public class Downlordtask 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 inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                int data = inputStreamReader.read();

                while (data !=-1){ char curretnt = (char) data; result += curretnt; data = inputStreamReader.read(); }  return  result;}
            catch (Exception e){e.printStackTrace();
                Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
            return null; }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);


            try {
                JSONObject jsonObject = new JSONObject(s);
                String wetherinfo = jsonObject.getString("weather");
                Log.i("weather",wetherinfo);
                JSONArray array = new JSONArray(wetherinfo);

                String message="";

                for (int i=0; i <array.length();i++){
                    JSONObject jsonPart = array.getJSONObject(i);

                    String main = jsonPart.getString("main");
                    String discrip = jsonPart.getString("description");

                    if (!main.equals("") && !discrip.equals("")){message += main + ":" + discrip + "\r\n";
                    }
                }

                    if(!message.equals("")) { textView2.setText(message);  }
            } catch (Exception e){e.printStackTrace();}
        }
    }

} ```

您不能从后台 thread.Use android OS 处理程序进行 ui 调用 所以替换

Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();

new Handler().post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();

        }
    });