2015-10-17 154 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