Creating a CrimeFragment


#1

Hi,

I’m having problems in trying to create a New --> Class --> CrimeFragment Fragment as what is discussed on page 139. When creating the class I don’t see any othre option for the Super Class other than “java.lang.Object”. That is the only Superclass available in the “Class” Wizard. Did I miss something or is there something I’m doing wrong?

Thanks,

Bob


#2

I found BlankFragment. Is that what I am supposed to use?


#3

Howdy Bob,

I’m not sure why the Fragment class isn’t showing up as an option in the new class wizard. Choosing the correct super class in that wizard isn’t terribly important.

You can always create a new class with the default super class and modify your super class in the code.

would become:

Just be sure to import the support library’s version of Fragments after making that change.

Chris.


#4

Chris,

Thanks for getting back to me. In eclypse, I found New --> Other --> Android Object --> New Blank Fragment. It created a file with all kinds of code including the code:

public class CrimeFragment extends Fragment {

Then I ran into a different situation

The Crime () was built in the Default Package folder and the CriminalFragment.java (New Blank Fragment) is in the com.bignerdranch.android.criminalintent folder with the
CrimeActivity.java file. In the CriminalFragment.java file I couldn’t get the command "private Crime mCrime; " to work (Syntax error) until I created a Crime.java class in the same folder. Should I move the Crime.java file in the same folder as the others or keep the same one which actually is an interfase?

New Crime.java file in the same folder as the CriminalFragment,java file:
public interface Crime {

}


#5

Hey Bob,

You should put everything in the same package/folder: com.bignerdranch.android.criminalintent. You can remove any extraneous Crime.java files that you created.

As you saw, you will run in to import issues if you create classes in the default package.

Packages in java are useful for organizing your code if you are creating a large application. In the book, we stick to one package for simplicity.


#6

Chris,

Does anything go into the Default Package folder? I really appreciate your help and I’m really happy with this book. I always wanted to learn this technology and since I became unemployed, I began studying this stuff. Its a lot different than COBOL on a mainframe.

Again thanks for getting back to me.

Bob


#7

Bob,

No problem. I’m happy to help.

You should never use the default package. Always put your code in some kind of named package.

By default, you can do what we do in the book and use the same package that you create when you initially create the project. If you’re not sure what that package is, you can always find it listed in your AndroidManifest.xml file.


#8

Chris,

I’ll remember that. I’m trying to go through the book right to the letter. Another issue I ran into in this chapter is when creating the CrimeActivity file I created. I ended up with syntax errors that say that I also need to create a “FragmentActivity” class. Is that what I need to do? This is what I have thus far:

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.Menu;

public class CrimeActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_crime);
}

}


#9

Bob,

It sounds like you just need to import the FragmentActivity class. Make sure the file is saved and then trigger the organize imports command.

To add more detail to what’s happening:

You can see the package that Android classes exist in by searching the documentation. FragmentActivity is here: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

So, the organize imports should add the following to your imports:


#10

Thats another issue I’ve been having. When I organized the imports I ended up with only one import. I’ve run into this before when I had to manually search then manually type in the correct import.

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;

public class CrimeActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_crime);
}

}


#11

That is interesting. That makes me think that there might be some kind of issue with your project set up.

You can see how the project is configured by right clicking on your project in the left pane of Eclipse and choosing properties.

I’d have to see more info about your project to know for sure, but my guess is that your project is not connected with the appropriate android libraries.


#12

Chris,

I didn’t see the “2” for the second page. What exactly do you need to see? There’s a lot of stuff there. I wouldn’t know where to begin, unless I was able to give you permission to connect to my PC. What do you want me to display?

Thanks,

Bob


#13

Bob,

First, I just want to point out that Eclipse will automatically collapse your imports so that they don’t take up extra space. The “plus” button to the left of your import will expand the imports to show them all (if there is a plus button). I just wanted to mention that in case you aren’t aware.

If that’s not the issue, your import problem may just be an Eclipse quirk. As a first step, I recommend restarting Eclipse and cleaning the project. See if that resolves the import issue.

If that does not work, there may be an issue with your classpath. Let me know what happens with the above steps. If they don’t work, I can expand on the classpath issue.

Chris.


#14

Chris,

I’m aware of the “+” buttons. I ran the “clean” again. The real test will be when I create a new class for Chapter 9. What happens is that at first when I add something in the code which requires an import, the “import” of the correct library isn’t one of the options that show up when you hover over the syntax error. What I then do is search for the correct import I need. Then once I type it in, eclypse identifies that the new “imput” I manually entered is needed and the syntax errors disappear. I can then “organize imports” and everything works fine.

Thanks,


#15

Hi Chris,

The above suggestions didn’t work. There is probably an issue with how I have the Classpath set up.


#16

Your issues make me wonder how your project might be setup. Could you take a screenshot of your project setup? Expand the src, gen, Android [version number], Android Private Libraries, and libs folders exactly one level (if they exist - if they don’t, don’t worry). Like this:

If you can’t post a screenshot, then if you can just confirm that you see android.jar and android-support-v4.jar as seen above, that would work, too.


#17

How do you do a printscreen of the dir structure? One thing I noticed is that all the files are in the private library. In the “lib” folder I have one “.jar” file.

libs
android-support-v4.jar


#18

Which files do you see where? Both android.jar and android-support-v4.jar are in “Android Private Libraries”?


#19

android private libraries
android-support-v4.jar
android-support-vr <— Where the lib files are
METF-INF

lib
android-support-v4.jar <— Empty file


#20

Typeo Thats “android.support.v4” where the files are located.