Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从线程返回空值时出现Android错误_Java_Android_Multithreading - Fatal编程技术网

Java 从线程返回空值时出现Android错误

Java 从线程返回空值时出现Android错误,java,android,multithreading,Java,Android,Multithreading,我创建此应用程序是为了将wether详细信息添加到我的应用程序中 我的Android应用程序中有一个GetWeather类,其中有一个返回字符串的GetWeather方法。但当我试图从由线程组成的类中获取值时。我总是得到一个空值。请参考我的代码,告诉我哪里出错了。多谢各位 主要活动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我创建此应用程序是为了将wether详细信息添加到我的应用程序中

我的Android应用程序中有一个GetWeather类,其中有一个返回字符串的GetWeather方法。但当我试图从由线程组成的类中获取值时。我总是得到一个空值。请参考我的代码,告诉我哪里出错了。多谢各位

主要活动

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);


                Button b1 = (Button) findViewById(R.id.showData);

                b1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {


                    GetWeather1 gw = new GetWeather1();
                    String weather = gw.GetWeather1 ("CA","Anuradhapura");

                    Toast.makeText(getApplicationContext(), " Weather condition is " + weather, Toast.LENGTH_SHORT).show();


                    }
                });
            }


    // This is GetWeather  class

        public class GetWeather {



            public String weather;
            public String temperature_string;
            public Bitmap weather_icon;

            public GetWeather() {

            }

            public  String GetWeather(String city, String state) {

                city = city.replaceAll(" ", "_");
                // construct post URL
                final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
                        + ".json";
                new Thread(new Runnable() {
                    public void run() {
                        String request = GET_WEATHER_URL;
                        HttpResponse rp = null;
                        JSONObject jObject = null;
                        try {
                            HttpClient httpclient = new DefaultHttpClient();
                            httpclient.getParams().setParameter(
                                    CoreProtocolPNames.PROTOCOL_VERSION,
                                    HttpVersion.HTTP_1_1);
                            HttpGet request1 = new HttpGet(
                                    "http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
                            HttpResponse response = httpclient.execute(request1);
                            HttpEntity resEntity = response.getEntity();
                            String _response = EntityUtils.toString(resEntity);
                            jObject = new JSONObject(_response);
                            JSONObject current_observation = jObject.getJSONObject("current_observation");
                            temperature_string = current_observation.getString("temperature_string");
                            weather = current_observation.getString("weather");
                            Log.i("..............", "" + weather);
                            Log.i("..............", "" + temperature_string);




                            String icon_url = current_observation.getString("icon_url");
                            weather_icon = get_weather_icon(icon_url);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }).start();

                 while (weather != null) {

                    }

                    return weather;


            }
条件
while(while!=null)
应该是
while(while==null)
等待
线程完成其任务。但它将在UI线程上等待,这可能会导致
ANR
错误

而是使用
AsyncTask
并在
onPostExecute()
中获取结果

关于你的评论,这可能会对你有所帮助

public class WeatherTask extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... params) {
        String city = params[0];
        String state = params[1];

        city = city.replaceAll(" ", "_");
        // construct post URL
        final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
                + ".json";
        String request = GET_WEATHER_URL;
        HttpResponse rp = null;
        JSONObject jObject = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(
                    CoreProtocolPNames.PROTOCOL_VERSION,
                    HttpVersion.HTTP_1_1);
            HttpGet request1 = new HttpGet(
                    "http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
            HttpResponse response = httpclient.execute(request1);
            HttpEntity resEntity = response.getEntity();
            String _response = EntityUtils.toString(resEntity);
            jObject = new JSONObject(_response);
            JSONObject current_observation = jObject
                    .getJSONObject("current_observation");
            String temperature_string = current_observation
                    .getString("temperature_string");
            String weather = current_observation.getString("weather");
            Log.i("..............", "" + weather);
            Log.i("..............", "" + temperature_string);
            String icon_url = current_observation.getString("icon_url");
            String weather_icon = get_weather_icon(icon_url);
            String[] out = new String[]{weather,weather_icon,temperature_string};
            return out;
        } catch (Exception exception) {

        }
        return null;
    }

    @Override
    protected void onPostExecute(String[] result) {
        if(result != null) {
            String weather = result[0];
            String weather_icon = result[1];
            String temperature_string = result[2];
        }
    }
}
条件
while(while!=null)
应该是
while(while==null)
等待
线程完成其任务。但它将在UI线程上等待,这可能会导致
ANR
错误

而是使用
AsyncTask
并在
onPostExecute()
中获取结果

关于你的评论,这可能会对你有所帮助

public class WeatherTask extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... params) {
        String city = params[0];
        String state = params[1];

        city = city.replaceAll(" ", "_");
        // construct post URL
        final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
                + ".json";
        String request = GET_WEATHER_URL;
        HttpResponse rp = null;
        JSONObject jObject = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(
                    CoreProtocolPNames.PROTOCOL_VERSION,
                    HttpVersion.HTTP_1_1);
            HttpGet request1 = new HttpGet(
                    "http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
            HttpResponse response = httpclient.execute(request1);
            HttpEntity resEntity = response.getEntity();
            String _response = EntityUtils.toString(resEntity);
            jObject = new JSONObject(_response);
            JSONObject current_observation = jObject
                    .getJSONObject("current_observation");
            String temperature_string = current_observation
                    .getString("temperature_string");
            String weather = current_observation.getString("weather");
            Log.i("..............", "" + weather);
            Log.i("..............", "" + temperature_string);
            String icon_url = current_observation.getString("icon_url");
            String weather_icon = get_weather_icon(icon_url);
            String[] out = new String[]{weather,weather_icon,temperature_string};
            return out;
        } catch (Exception exception) {

        }
        return null;
    }

    @Override
    protected void onPostExecute(String[] result) {
        if(result != null) {
            String weather = result[0];
            String weather_icon = result[1];
            String temperature_string = result[2];
        }
    }
}
条件
while(while!=null)
应该是
while(while==null)
等待
线程完成其任务。但它将在UI线程上等待,这可能会导致
ANR
错误

而是使用
AsyncTask
并在
onPostExecute()
中获取结果

关于你的评论,这可能会对你有所帮助

public class WeatherTask extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... params) {
        String city = params[0];
        String state = params[1];

        city = city.replaceAll(" ", "_");
        // construct post URL
        final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
                + ".json";
        String request = GET_WEATHER_URL;
        HttpResponse rp = null;
        JSONObject jObject = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(
                    CoreProtocolPNames.PROTOCOL_VERSION,
                    HttpVersion.HTTP_1_1);
            HttpGet request1 = new HttpGet(
                    "http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
            HttpResponse response = httpclient.execute(request1);
            HttpEntity resEntity = response.getEntity();
            String _response = EntityUtils.toString(resEntity);
            jObject = new JSONObject(_response);
            JSONObject current_observation = jObject
                    .getJSONObject("current_observation");
            String temperature_string = current_observation
                    .getString("temperature_string");
            String weather = current_observation.getString("weather");
            Log.i("..............", "" + weather);
            Log.i("..............", "" + temperature_string);
            String icon_url = current_observation.getString("icon_url");
            String weather_icon = get_weather_icon(icon_url);
            String[] out = new String[]{weather,weather_icon,temperature_string};
            return out;
        } catch (Exception exception) {

        }
        return null;
    }

    @Override
    protected void onPostExecute(String[] result) {
        if(result != null) {
            String weather = result[0];
            String weather_icon = result[1];
            String temperature_string = result[2];
        }
    }
}
条件
while(while!=null)
应该是
while(while==null)
等待
线程完成其任务。但它将在UI线程上等待,这可能会导致
ANR
错误

而是使用
AsyncTask
并在
onPostExecute()
中获取结果

关于你的评论,这可能会对你有所帮助

public class WeatherTask extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... params) {
        String city = params[0];
        String state = params[1];

        city = city.replaceAll(" ", "_");
        // construct post URL
        final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
                + ".json";
        String request = GET_WEATHER_URL;
        HttpResponse rp = null;
        JSONObject jObject = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(
                    CoreProtocolPNames.PROTOCOL_VERSION,
                    HttpVersion.HTTP_1_1);
            HttpGet request1 = new HttpGet(
                    "http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
            HttpResponse response = httpclient.execute(request1);
            HttpEntity resEntity = response.getEntity();
            String _response = EntityUtils.toString(resEntity);
            jObject = new JSONObject(_response);
            JSONObject current_observation = jObject
                    .getJSONObject("current_observation");
            String temperature_string = current_observation
                    .getString("temperature_string");
            String weather = current_observation.getString("weather");
            Log.i("..............", "" + weather);
            Log.i("..............", "" + temperature_string);
            String icon_url = current_observation.getString("icon_url");
            String weather_icon = get_weather_icon(icon_url);
            String[] out = new String[]{weather,weather_icon,temperature_string};
            return out;
        } catch (Exception exception) {

        }
        return null;
    }

    @Override
    protected void onPostExecute(String[] result) {
        if(result != null) {
            String weather = result[0];
            String weather_icon = result[1];
            String temperature_string = result[2];
        }
    }
}


谢谢,先生。如何返回这两个值。先生,我想从表示天气和温度的线程中得到这两个值。这里可以吗?谢谢,先生。我试试看。再次感谢你,因为我对这门课感到困惑。我所做的是GetWeather gw=newgetweather();WeatherTask()wc=new WeatherTask().execute(“CA”,“Anuradhapura”);Toast.makeText(getApplicationContext(),“天气条件为”+天气,Toast.LENGTH\u SHORT).show();但它对我不起作用。我必须实例化AsynkTask类吗?请回答我,先生。如何返回这两个值。先生,我想从表示天气和温度的线程中得到这两个值。这里可以吗?谢谢,先生。我试试看。再次感谢你,因为我对这门课感到困惑。我所做的是GetWeather gw=newgetweather();WeatherTask()wc=new WeatherTask().execute(“CA”,“Anuradhapura”);Toast.makeText(getApplicationContext(),“天气条件为”+天气,Toast.LENGTH\u SHORT).show();但它对我不起作用。我必须实例化AsynkTask类吗?请回答我,先生。如何返回这两个值。先生,我想从表示天气和温度的线程中得到这两个值。这里可以吗?谢谢,先生。我试试看。再次感谢你,因为我对这门课感到困惑。我所做的是GetWeather gw=newgetweather();WeatherTask()wc=new WeatherTask().execute(“CA”,“Anuradhapura”);Toast.makeText(getApplicationContext(),“天气条件为”+天气,Toast.LENGTH\u SHORT).show();但它对我不起作用。我必须实例化AsynkTask类吗?请回答我,先生。如何返回这两个值。先生,我想从表示天气和温度的线程中得到这两个值。这里可以吗?谢谢,先生。我试试看。再次感谢你,因为我对这门课感到困惑。我所做的是GetWeather gw=newgetweather();WeatherTask()wc=new WeatherTask().execute(“CA”,“Anuradhapura”);Toast.makeText(getApplicationContext(),“天气条件为”+天气,Toast.LENGTH\u SHORT).show();但它对我不起作用。我必须实例化AsynkTask类吗?请回答我,先生