This is an old revision of the document!
Table of Contents
Chapter 10
Under development.
p 401: "Fragment name is not a valid class name"
There is a subtlety in creating new Android fragments in Android Studio that Android Studio does a horrible job of negotiating.
If you try to create a new Blank fragment (File > New > Fragment > Fragment (Blank)) without initially proving some context regarding in what package the fragment should be generated, you may/will get a “Fragment name is not a valid class name” error. The simplest solution is to select the package in the left had project navigator pane and then create the fragment.
p 401: Proper inheritance
Android Studio might generate code that makes your new fragment derive from android.support.v4.app.Fragment
rather than android.app.Fragment
. That's not what we want.
Make sure the fragment's java file imports android.app.Fragment
rather than android.support.v4.app.Fragment
and that the class extends Fragment.
The above applies to all the fragments you'll create in this chapter.
p 401: Project dependencies
In the preceding chapter, you were instructed to remove the project's dependency on com.android.support:appcompat-v7:<whatever>
and/or com.android.support:appcompat-v4:<whatever>
to work around an Android Studio bug. However, the DrawerLayout that we will use in a bit requires the v4 compat libs.
Leave com.android.support:appcompat-v7:<whatever>
as a project dependency or add it back if you removed it.
A dependence on com.android.support:appcompat-v7:<whatever>
will automatically bring in v4
.
p 401: Code blocks
Here's what I ended up with:
- TopFragment.java
package com.hfad.bitsandpizzas; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. */ public class TopFragment extends Fragment { public TopFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_top, container, false); } }
Notice the required empty public constructor not included in the book's code.
The official docs say “All subclasses of Fragment must include a public no-argument constructor.” The book doesn't follow this practice, but you should.
And here's the layout:
- fragment_top.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title_top" /> </RelativeLayout>
Android studio might set tools:context
to some value other than .MainActivity
. Be sure to make the change.
p. 408-415
The exposition here gets a little squirrelly and the “updated MainActivity.java” isn't all quite there, so here's what MainActivity.java should look like at the end of p. 415: