我正在做一個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);
}
}
}
}
}
你已經發布了代碼,但是你沒有發佈你收到的錯誤。 –
對不起。錯誤出現在第二個到最後一個}它說return statement缺失。 – Jan