These days, I have been reading through Android Developer training document(http://developer.android.com/training/index.html) in detail and playing with samples. I am in the Fragment section now and I found out an example app has an error. Here is the link:
http://developer.android.com/shareables/training/FragmentBasics.zip
I give you a brief about the example.
Entry point of the app is MainActivity and it uses different layouts depending on the screen sizes layout, one for normal and the other for large size devices. You can see only news article list when you run the app on handset devices but it will display two pane layout on large size device such as 7" tablets.
/res/layout/news_article.xml(Normal size)
http://developer.android.com/shareables/training/FragmentBasics.zip
I give you a brief about the example.
Entry point of the app is MainActivity and it uses different layouts depending on the screen sizes layout, one for normal and the other for large size devices. You can see only news article list when you run the app on handset devices but it will display two pane layout on large size device such as 7" tablets.
/res/layout/news_article.xml(Normal size)
/res/layout-large/new_article.xml(Large size)
It will load news content when you click a item from the headlines. The MainActivity switches Fragment to display it in a handset but it displays news content in the second pane in a table device. Error comes from ArticleFragment class. When the MainActivity loads two pane news_articles.xml in /res/layout-large, it cannot find view having ID 'article' because it tried to find the view by calling
getActivity().findViewById()
. This does not work. You have to read the article TextView from rootView of the article_view.xml. So fixed code should be like this:
public class ArticleFragment extends Fragment { TextView articleText; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // If activity recreated (such as from screen rotate), restore // the previous article selection set by onSaveInstanceState(). // This is primarily necessary when in the two-pane layout. if (savedInstanceState != null) { mCurrentPosition = savedInstanceState.getInt(ARG_POSITION); } // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.article_view, container, false); articleText = (TextView) rootView.findViewById(R.id.article); return rootView; } } ... public void updateArticleView(int position) { //TextView article = (TextView) getActivity().findViewById(R.id.article); -> This causes error. if (articleText != null) articleText.setText(Ipsum.Articles[position]); mCurrentPosition = position; }You can clone the fixed source code from Git.(https://github.com/krpot/FragmentBasic)
Comments
Post a Comment