Skip to main content

Google's fragment example error

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)
 
/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

Popular posts from this blog

How to test AsyncTask in Android

In Android, test is not as easy as any other platform. Because Android test cannot be run without emulator. Particulary when it comes to AsyncTask or Service, it is difficult to test because they are different type of thread and hard to check their result. Then, how can we ensure the result of AsyncTask valid? AsyncTask is a thread and an asynchnorous as the name means. So, we need to wait for it finishes its job and need to capture the event. Then, when it happens in AsyncTask. It can be one of onBackground() and onPostExecute() methods. It doesn't matter you use onBackground() or onPostExecute() but I prefer onPostExecute(). Anyway, we can test an AsyncTask if we can hook it. Then, how can we hook it? For that, we can use callback pattern. But we need to delay main thread to wait for the AsyncTask's job done because we want to check the result. So the structure for the test would be like: 1. Create AsyncTask A 2. Injection a callback into A 3. Wait until A finish 4....

Apply Kotlin DataBinding to Android Studio Generated Main Activity

I posted how to setup Kotlin and DataBinding in Android Stuido in the last blog (http://marksunghunpark.blogspot.com.au/2017/04/kotlin-binding-library-setup-in-android.html). In this post, I am going to how to use DataBiding in the MainActivity when you create a navigation drawer project from Android Studio templates. Layouts You will have four layouts in app/src/res/layout folder: app/src/main/res/layout/activity_main.xml app/src/main/res/layout/app_bar_main.xml app/src/main/res/layout/content_main.xml app/src/main/res/layout/nav_header_main.xml And activity_main.xml contains all other layout using include layout. You need to have tag in activity_main.xml , app_bar_main.xml and content_main.xml . If you don't have the tag, Binding library cannot recognise the sub layouts properly. Binding library doesn't support toolbar and navigation drawer yet, so you can use using BindingAdapter if you want to use binding library.(But I'm gong to skip this part for simplici...

Fragment LifeCycle

Normal setRetainInstance(true) Description onAttach onAttach Called once the fragment is associated with its activity. onCreate - Called to do initial creation of the fragment. onCreateView onCreateView Creates and returns the view hierarchy associated with the fragment. onActivityCreated onActivityCreated Tells the fragment that its activity has completed its own Activity.onCreate(). onViewStateRestored onViewStateRestored Tells the fragment that all of the saved state of its view hierarchy has been restored. onStart onStart Makes the fragment visible to the user (based on its containing activity being started). onResume onResume Makes the fragment begin interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series...