ClassNotFoundException


#1

Hi,
I’m VERY new to Android programming and am carefully working my way through your book.
(I’m trying to not use the support for fragments for pre-fragment Android versions, following note at bottom of page147)

I’m getting the ClassNotFoundException for my CrimeActivity class:

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;

import com.example.criminalintent.R;

public class CrimeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crime);
		FragmentManager fm = getFragmentManager();
		Fragment fragment =fm.findFragmentById(R.id.fragmentContainer);
		
		if(fragment==null){
			fragment = new CrimeFragment();
			fm.beginTransaction().add(R.id.fragmentContainer,fragment).commit();
		}
		
	}


}

I’ve copied the changes in the AndroidManifest.xml as below. There don’t appear to be any compiler errors? Can you help or do you need more info?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.criminalintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

   
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name=".CrimeActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>

I hope its OK to send this little bit of code?

Cheers

Brian


#2

Delete 'import com.example.criminalintent.R;’ and hopefully it will work. Quite often if there’s a mistake in XML it will try and import R. One of the many annoying quirks of the Eclipse plug-in.


#3

Martin - Thanks for your help. Unfortunately when I delete ‘import com.example.criminalintent.R;’ it leaves R unresolved in 3 places in the code. I tried using ‘import android.R’ instead, but that leaves activity_crime and fragmentContainer unresolved. Do you have any other ideas? Would you like to see the rest of the code?
Thanks again for looking at this for me.

Cheers

Brian


#4

At a guess, there might be an error in your XML. The import R is an annoying bug in Eclipse, which every time I’ve had its been an error in my XML and the import hot key has tried to resolve. I cant guarantee it’ll will fix it, but go over your layout code again and delete import R. Good luck =)

Edit: The error will probably be in your layout XML and not your manifest.


#5

Martin,
Thanks for your help. Sorry I didn’t get back earlier. I decided I’d better re-work my code and use the support library, which I’ve now done and it is working successfully. The book is absolutely the best Android learning tool I’ve seen. I’m enjoying every minute of it!

Cheers

Brian

PS I have the first edition - Are there significant changes in the later editions?