2015-10-17 155 views
2

我正在做一个android应用程序。并突然出现这个错误, 错误是什么?请帮我:(
我使用和Android Studio IDE中 虽然我下面的说明我想不出什么似乎是问题。我很新的环境也。在android studio上缺少返回语句

/** 
* A placeholder fragment containing a simple view. 
*/ 
public class MainActivityFragment extends Fragment { 

    private ArrayAdapter<String> mForecastAdapter; 

    public MainActivityFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     String[] forecastArray = { 
       "Today - Foggy - 12/3", 
       "Tomorrow - Rainy - 7/4", 
       "Wed - Sunny - 12/3", 
       "Thurs - Cloudy - 10/4", 
       "Fri - Rainy - 12/8", 
       "Sat - Heavy Rain- 10/5", 
       "Sun - Sunny - 32/23" 

     }; 
     List<String> weekforecast = new ArrayList<String>(
       Arrays.asList(forecastArray)); 

     mForecastAdapter = 
       new ArrayAdapter<String>(
         getActivity(), 
         R.layout.list_item_forecast, 
         R.id.list_item_forecast_textview, 

     weekforecast); 
     ListView listView = (ListView) rootView.findViewById(R.id.list_item_forecast); 
     listView.setAdapter(mForecastAdapter); 

     return rootView; 
    } 

    public class FetchWeatherTask extends AsyncTask<Void, Void, Void> { 
     private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); 

     @Override 
     protected Void doInBackground(Void... params) { 
      HttpURLConnection urlConnection = null; 
      BufferedReader reader = null; 

      // Will contain the raw JSON response as a string. 
      String forecastJsonStr = null; 

      try { 
       // Construct the URL for the OpenWeatherMap query 
       // Possible parameters are avaiable at OWM's forecast API page, at 
       // http://openweathermap.org/API#forecast 
       URL url = new URL("api.openweathermap.org/data/2.5/find?q=7000&mode=json&units=metric&ch+7&appid=bd82977b86bf27fb59a04b61b657fb6f"); 

       // Create the request to OpenWeatherMap, and open the connection 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 
       urlConnection.connect(); 

       // Read the input stream into a String 
       InputStream inputStream = urlConnection.getInputStream(); 
       StringBuffer buffer = new StringBuffer(); 
       if (inputStream == null) { 
        // Nothing to do. 
        return null; 
       } 
       reader = new BufferedReader(new InputStreamReader(inputStream)); 

       String line; 
       while ((line = reader.readLine()) != null) { 
        // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) 
        // But it does make debugging a *lot* easier if you print out the completed 
        // buffer for debugging. 
        buffer.append(line + "\n"); 
       } 

       if (buffer.length() == 0) { 
        // Stream was empty. No point in parsing. 
        return null; 
       } 
       forecastJsonStr = buffer.toString(); 
      } catch (IOException e) { 
       Log.e("PlaceholderFragment", "Error ", e); 
       // If the code didn't successfully get the weather data, there's no point in attemping 
       // to parse it. 
       return null; 
      } finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
       if (reader != null) { 
        try { 
         reader.close(); 
        } catch (final IOException e) { 
         Log.e("PlaceholderFragment", "Error closing stream", e); 
        } 
       } 
      } 

    } 
} 
+0

你已经发布了代码,但是你没有发布你收到的错误。 –

+0

对不起。错误出现在第二个到最后一个}它说return statement缺失。 – Jan

回答

1

您应该使用异步类出方,这样的: 编辑: 最后经过加回空

public class MainActivityFragment extends Fragment { 

private ArrayAdapter<String> mForecastAdapter; 

public MainActivityFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    String[] forecastArray = { 
      "Today - Foggy - 12/3", 
      "Tomorrow - Rainy - 7/4", 
      "Wed - Sunny - 12/3", 
      "Thurs - Cloudy - 10/4", 
      "Fri - Rainy - 12/8", 
      "Sat - Heavy Rain- 10/5", 
      "Sun - Sunny - 32/23" 

    }; 
    List<String> weekforecast = new ArrayList<String>(
      Arrays.asList(forecastArray)); 

    mForecastAdapter = 
      new ArrayAdapter<String>(
        getActivity(), 
        R.layout.list_item_forecast, 
        R.id.list_item_forecast_textview, 

        weekforecast); 
    ListView listView = (ListView) rootView.findViewById(R.id.list_item_forecast); 
    listView.setAdapter(mForecastAdapter); 

    return rootView; 
} 


public class FetchWeatherTask extends AsyncTask<Void, Void, Void> 


{ 


    private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); 

    @Override 
    protected Void doInBackground(Void... params) { 
     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 

     // Will contain the raw JSON response as a string. 
     String forecastJsonStr = null; 

     try { 
      // Construct the URL for the OpenWeatherMap query 
      // Possible parameters are avaiable at OWM's forecast API page, at 
      // http://openweathermap.org/API#forecast 
      URL url = new URL("api.openweathermap.org/data/2.5/find?q=7000&mode=json&units=metric&ch+7&appid=bd82977b86bf27fb59a04b61b657fb6f"); 

      // Create the request to OpenWeatherMap, and open the connection 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = urlConnection.getInputStream(); 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       // Nothing to do. 
       return null; 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) 
       // But it does make debugging a *lot* easier if you print out the completed 
       // buffer for debugging. 
       buffer.append(line + "\n"); 
      } 

      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       return null; 
      } 
      forecastJsonStr = buffer.toString(); 
     } catch (IOException e) { 
      Log.e("PlaceholderFragment", "Error ", e); 
      // If the code didn't successfully get the weather data, there's no point in attemping 
      // to parse it. 
      return null; 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e("PlaceholderFragment", "Error closing stream", e); 
       } 
      } 
     } 
     return null; 

    } 
} 

}

+0

它仍然输出相同的错误。我在第二个到最后一个}之前添加了一个返回null,我认为它确定。但我不知道为什么。顺便谢谢。 @Shahar – Jan

+0

错过了...你应该在finally {}之后返回null。 – Shahar

+0

谢谢@Shahar :) – Jan

0

正如你可以看到重。转方法Void doInBackground(Void... params)Void而不是void。它不是原始的void,而是代表它的类,以相同的方式Integer类代表int

它是您的方法的类和返回类型,您需要在所有执行路径的末尾添加return语句。在你的方法中,如果执行路径转到finally子句,那么没有返回语句并因此出现错误。

+0

谢谢@Abdullah – Jan