This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.06 Hook the adapter up to the ListView
- Loading branch information
Lyla
committed
Mar 4, 2015
1 parent
0281f77
commit 1d42493
Showing
1 changed file
with
6 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still current when I create the project I get a separate main activity fragment with a single fragment inside as such I've changed
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
To
ListView listView = (ListView) getActivity().findViewById(R.id.listview_forecast);
Which causes a force close
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran a test for you. That's because getActivity().findViewById returned null.
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@martipello did you create rootView?
See line 104:
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
(and then return it in line 110).
The code is still valid if you create
rootView
like that and then return it at the end ofonCreateView
method.1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to check content of fragment_main.xml for existing the following line (in ListView control):
android:id="@+id/listview_forecast"
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If anyone else encounters this problem, I had a similar situation.
The layouts_v11 folder contains another xml file named fragment_main.xml. This is the one we played with in our first UI exercise (fiddling with design mode). Though I am unfamiliar with all this, I imagine that this folder and the files within are used in adapting to different screen sizes. Since my test device is a Nexus 7 tablet, the v11 fragment_main was being called in MainActivity.java, and did not contain the ListView element/id. Thus the null pointer.
Either renaming this file or changing it to do what we want it to should resolve the issue. Since I don't know how to adapt a UI to another screen size, I renamed the file. Code now progresses as expected.
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In android 1.4, I get two java files mainactivity and mainactivity fragment. I couldnt find any placeholder fragment. Also I am getting an error in rootview (Rootview is never assigned). What should I do now
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For your mainactivity fragment, edit the code like this and it will work:
`public class MainActivityFragment extends Fragment {
}`
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can someone please help me out... "R.id.container" the container id belongs to where?
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
/Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);/
}
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
R.id.container refers to the ID of a View in your MainActivity's layout file, which contains the fragment.
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please help. Getting this exception:
" java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
`public static class PlaceholderFragment extends Fragment {
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello everyone! Can you please help me?
At this point, I am getting a null pointer exception and I cannot understand why... It's accused in the following line of MainActivity:
"listView.setAdapter(mForecastAdapter);"
Thanks in advance!
1d42493
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yvasilyev92 , How have you defined your activity_main.xml ? Just these lines will throw up error: