我进入吐司后我的天气应用程序崩溃了

StudyFromHome:

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

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

工作代码:::

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();}
        }
    }

} ```

汤普森斯巴达:

您无法从backgroud线程进行ui调用。请使用android OS处理程序,因此请替换

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();

        }
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的应用程序崩溃了

10次尝试后我的android应用程序崩溃

声明RealmSwift类后,我的应用程序崩溃

添加MediaRouteButton后,我的应用程序崩溃

覆盖 onSaveInstanceState 后,我的应用程序不断崩溃

firebase 更新了重大更改后,我的应用程序崩溃了

添加OnclickListener后,我的Android应用程序崩溃

单击按钮后我的应用程序崩溃

我的应用程序不会显示“ KitKat”吐司消息

我正在开发 kivymd 应用程序,但应用程序在获取权限后崩溃

关闭起始子对话框并进入父对话框后,我的 qt 桌面应用程序不断崩溃

如何在我的天气应用程序上使用天气图标

RelativeLayout onClickListener使我的应用程序崩溃

linearGradientProps 使我的应用程序崩溃

我的应用程序不断崩溃

setOnClickListener使我的Wear应用程序崩溃

我的应用程序不断崩溃

UIModalTransitionStylePartialCurl使我的应用程序崩溃

为什么我的应用程序崩溃了?

我的 android 应用程序不断崩溃

我的应用程序在MainActivity中崩溃

onSaveInstanceState使我的应用程序崩溃

_tableView reloadData使我的应用程序崩溃

setOnClickListener(this)使我的应用程序崩溃

分页使我的应用程序崩溃

onBackPressed()使我的Webview应用程序崩溃

我按下“使用Facebook登录”后,我的应用程序崩溃React-native fbsdk

应用程序进入applicationDidEnterBackground后,我们应该怎么做才能运行NSoperationqueue?

在我将设置活动连接到我的 Activity2 后,我的应用程序崩溃了