Comments (30)
Does the onCreate method of your ForecastFragment contain the statement setHasOptionsMenu(true); ?
from sunshine-version-2.
It looks like I do not. Where should I add this?
package com.dev.bao.sunshine;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ForecastFragment extends Fragment {
ArrayAdapter<String> mForecastAdapter;
public ForecastFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] data = {
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thurs 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
"Sun 6/29 - Sunny - 20/7",
"Mon 6/30 - Sunny - 45/40",
"Tue 7/1 - Rainy - 30/18"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
mForecastAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekForecast);
View rootView = inflater.inflate(R.layout.activity_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
public class FetchWeatherTask extends AsyncTask<Void, Void, Void> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
@Override
protected Void doInBackground(Void... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
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
String baseUrl = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901";
String apiKey = "&APPID=" + BuildConfig.OPEN_WEATHER_MAP_API_KEY;
URL url = new URL(baseUrl.concat(apiKey));
// 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(LOG_TAG, "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(LOG_TAG, "Error closing stream", e);
}
}
}
return null;
}
}
}
from sunshine-version-2.
You are trying to have the the fragment add an item to the overflow menu, yes?
In the ForecastFragment class override the onCreate method and write the statement setHasOptionsMenu(true); .
from sunshine-version-2.
Hi, sorry I am honestly not sure. I am completely new to android
development. I am currently on Lesson 2, adding a refresh button in
ForecastFragment.java
This is the section
2.02_refactor_forecast_fragment...2.03_add_refresh_xml
I tried the above and debugged, I still did not see the menu option.
Thank You,
Bao Thai
On Sat, Dec 26, 2015 at 2:50 PM, Kasijjuf [email protected] wrote:
You are trying to have the the fragment add an item to the overflow menu,
yes?In the ForecastFragment class override the onCreate method and write the
statement setHasOptionsMenu(true); .—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
What's in your forecastfragment.xml file? If you upload your code to a public repository in your GitHub account, I can give you better help.
from sunshine-version-2.
Sorry, I am not home right now. Will you be free in 3 or 4 hours?
On Dec 26, 2015, at 4:11 PM, Kasijjuf [email protected] wrote:
What's in your forecastfragment.xml file? If you upload your code to a public repository in your GitHub account, I can give you better help.
—
Reply to this email directly or view it on GitHub.
from sunshine-version-2.
I should be.
from sunshine-version-2.
I am still messing around with GitHub trying to figure out how to upload
codes and etc... However I'll just paste my forecastfragment.xml code below
On Sat, Dec 26, 2015 at 4:20 PM, Kasijjuf [email protected] wrote:
I should be.
—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
OK, I think I figured out why the menu isn't displaying. You've added the XML that defines the menu, but the code that actually reads and implements that definition hasn't been added yet. That's the job of the next step:
2.03_add_refresh_xml...2.04_inflate_menu
from sunshine-version-2.
Hmmm, I did that, and I still do not see it.
package com.dev.bao.sunshine;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class ForecastFragment extends Fragment {
ArrayAdapter<String> mForecastAdapter; public ForecastFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add this line in order for this fragment to handle menu events. setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.forecastfragment, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_refresh) { return true; } return super.onOptionsItemSelected(item); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setHasOptionsMenu(true); // Create some dummy data for the ListView. Here's a sample weekly forecast String[] data = { "Mon 6/23 - Sunny - 31/17", "Tue 6/24 - Foggy - 21/8", "Wed 6/25 - Cloudy - 22/17", "Thurs 6/26 - Rainy - 18/11", "Fri 6/27 - Foggy - 21/10", "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18", "Sun 6/29 - Sunny - 20/7", "Mon 6/30 - Sunny - 45/40", "Tue 7/1 - Rainy - 30/18" }; List<String> weekForecast = new ArrayList<String>(Arrays.asList(data)); mForecastAdapter = new ArrayAdapter<String>( getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast); View rootView = inflater.inflate(R.layout.activity_main, container, false); ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast); listView.setAdapter(mForecastAdapter); return rootView; } public class FetchWeatherTask extends AsyncTask<Void, Void, Void> { private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); HttpURLConnection urlConnection = null; BufferedReader reader = null; // Will contain the raw JSON response as a string. String forecastJsonStr = null; @Override protected Void doInBackground(Void... params) { // These two need to be declared outside the try/catch // so that they can be closed in the finally block. 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 String baseUrl = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901"; String apiKey = "&APPID=" + BuildConfig.OPEN_WEATHER_MAP_API_KEY; URL url = new URL(baseUrl.concat(apiKey)); // 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(LOG_TAG, "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(LOG_TAG, "Error closing stream", e); } } } return null; } } }
On Sat, Dec 26, 2015 at 7:29 PM, Kasijjuf [email protected] wrote:
OK, I think I figured out why the menu isn't displaying. You've added the
XML that defines the menu, but the code that actually reads and implements
that definition hasn't been added yet. That's the job of the next step:2.03_add_refresh_xml...2.04_inflate_menu
2.03_add_refresh_xml...2.04_inflate_menu—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
Both your onCreate method and your onCreateView method contain the
setHasOptionsMenu(true);
statement. It should only be in onCreate.
from sunshine-version-2.
I did that and it's still the same.
Is it because of the way my activity_main.xml is set up? I feel like it is.
from sunshine-version-2.
You are running this on an actual device or on the emulator and not just looking at what appears in the Design view, right?
from sunshine-version-2.
Yes, I am running it on an emulator. I attempted to delete the toolbar from main_activity.xml, it's gone, but the option settings bar is still nowhere. I'm just honestly messing around and see what is the problem.
On Dec 28, 2015, at 1:27 PM, Kasijjuf [email protected] wrote:
You are running this on an actual device or on the emulator and not just looking at what appears in the Design view, right?
—
Reply to this email directly or view it on GitHub.
from sunshine-version-2.
I am using SDK 16 in case you wondered.
from sunshine-version-2.
As your minSdkVersion?
from sunshine-version-2.
Yes
On Dec 28, 2015, at 2:34 PM, Kasijjuf [email protected] wrote:
As your minSdkVersion?
—
Reply to this email directly or view it on GitHub.
from sunshine-version-2.
If you upload your code to a public repository in your GitHub account, I can give you better help. Until then I'm stumped.
from sunshine-version-2.
May you guide me with this? I am trying to use the desktop version.
On Mon, Dec 28, 2015 at 11:20 PM, Kasijjuf [email protected] wrote:
If you upload your code to a public repository in your GitHub account, I
can give you better help. Until then I'm stumped.—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
Oooooh, I think I got it now, which files do you need from my android
studio?
See if you can check this git hub https://github.com/exocore123/sunshine
On Mon, Dec 28, 2015 at 11:24 PM, Bao Thai [email protected] wrote:
May you guide me with this? I am trying to use the desktop version.
On Mon, Dec 28, 2015 at 11:20 PM, Kasijjuf [email protected]
wrote:If you upload your code to a public repository in your GitHub account, I
can give you better help. Until then I'm stumped.—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
Sorry for the delayed response,
In Android Studio open your project, then in the menu bar select VCS -> Import into Version Control -> Share Project on GitHub.
from sunshine-version-2.
https://github.com/exocore123/Sunshine2
from sunshine-version-2.
Ok I think I figured it out for real this time.
First, compiling and running the code as you have it in your initial commit on your GitHub repository produces an app with no action bar. To add the action bar back simply remove the attribute android:theme="@style/AppTheme.NoActionBar" from line 14 of your AndroidManifest.xml file (link).
Now I am hypothesizing that when you created your Android Virtual Device, you selected either the Nexus One or the Nexus S as the base template. Both of these devices are pre-Honeycomb and thus have the pre-Honeycomb-style navigation buttons, i.e. Back, Menu, Home, and Search. By default when an app is run on such a device, the menu options are accessed via the Menu button rather than via an overflow button in the action bar. I'm fairly certain there is a way to override this behaviour, but I am unaware of what that way is.
When I ran your app in an Android Virtual Device using the Galaxy Nexus as the base template, the overflow button appeared in the action bar as expected (after performing the operation described in paragraph two, of course).
Did I get it right this time?
from sunshine-version-2.
Wow, you got it! So selecting an android device emulator does matter? Do
you have any recommendation when I set up as a beginner android app
development? That was a rather bugging issue when it was so minor. Thank
you so much!
On Thu, Dec 31, 2015 at 10:55 PM, Kasijjuf [email protected] wrote:
Ok I think I figured it out for real this time.
First, compiling and running the code as you have it in your initial
commit on your GitHub repository produces an app with no action bar. To add
the action bar back simply remove the attribute
android:theme="@style/AppTheme.NoActionBar" from line 14 of your
AndroidManifest.xml file (link
https://github.com/exocore123/Sunshine2/blob/master/app/src/main/AndroidManifest.xml#L14
).Now I am hypothesizing that when you created your Android Virtual Device,
you selected either the Nexus One or the Nexus S as the base template. Both
of these devices are pre-Honeycomb and thus have the pre-Honeycomb-style
navigation buttons, i.e. Back, Menu, Home, and Search. By default when an
app is run on such a device, the menu options are accessed via the Back
button rather than via an overflow button in the action bar. I'm fairly
certain there is a way to override this behaviour, but I am unaware of what
that way is.When I ran your app in an Android Virtual Device using the Galaxy Nexus as
the base template, the overflow button appeared in the action bar as
expected (after performing the operation described in paragraph two, of
course).Did I get it right this time?
—
Reply to this email directly or view it on GitHub
#109 (comment)
.
from sunshine-version-2.
Thanks for the challenge and glad I could help. Keep in touch.
from sunshine-version-2.
@Kasijjuf This helped me too. Thank u brother
from sunshine-version-2.
@Laksh05 Glad to hear it
from sunshine-version-2.
my app is crashing
what does container mean in this code? I am getting error in R.id.container
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container,new ForecastFragment()).commit();
}
}
from sunshine-version-2.
@pri10 The specific view that will hold the ForecastFragment()
after attaching. Similar relationship to Picture Frame and the Picture.
from sunshine-version-2.
Okay thanks :) @elmargomez
from sunshine-version-2.
Related Issues (20)
- Issue in ForecastFragment FORECAST_COLUMNS and indices - Location_id column is missing HOT 1
- when i open it HOT 3
- UnsupportedMethodException
- Does not sync Automatically - works when refreshed or location change
- gradle version causes AS to fail build HOT 2
- No icon on the action bar!
- minSDK too low
- Refresh button not responding after adding code HOT 5
- addpreferencesfromresource and findPreference deprecated HOT 7
- error in mainActivity
- SettingsActivity didn't save location's value when I changed this HOT 1
- 2.01 add the Network Code HOT 1
- Adding Images to the App Resources$NotFoundException Resource ID #0xffffffff HOT 1
- request: Original SVG / Vector drawables HOT 1
- Listview only syncing when activity restarts HOT 1
- E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
- How to add a refresh button? HOT 1
- HOW TO CREATE FRAGMENT IN ANDROID STUDIO 3.0.1
- Installed API and updated with an error
- Could not find method implementation() for arguments [directory 'libs'] on project ':app'.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sunshine-version-2.