0

I am fairly new to android programming and I'm working off an example from a book I purchased but the example app they are showing me how to make is coming up with bugs in it. Its very simple so far but I am getting the following error so far:

07-30 04:36:59.265: W/System.err(540): java.lang.ClassNotFoundException: com.sanbox.basicsstarter.LifeCycleTest
07-30 04:36:59.285: W/System.err(540):  at java.lang.Class.classForName(Native Method)
07-30 04:36:59.285: W/System.err(540):  at java.lang.Class.forName(Class.java:217)

My code is really on 3 seperate files the first is the main activity calling the AndroidBasicsStarterActivity:

package com.sandbox.basicsstarter;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.util.Log;

public class AndroidBasicsStarterActivity extends ListActivity {
String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest",
"KeyTest", "AccelerometerTest", "AssetsTest",
"ExternalStorageTest", "SoundPoolTest", "MediaPlayerTest",
"FullScreenTest", "RenderViewTest", "ShapeTest", "BitmapTest",
"FontTest", "SurfaceViewTest" };
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, tests));
}

private void log(String text) {
    Log.d("LifeCycleTest ", text);
}

@Override
protected void onListItemClick(ListView list, View view, int position,
    long id) {
        super.onListItemClick(list, view, position, id);
        String testName = tests[position];
        log(testName);
    try {
        Class clazz = Class.forName("com.sanbox.basicsstarter." + testName);

        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
}

then the LifeCycleTest class:

package com.sandbox.basicsstarter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LifeCycleTest extends Activity {
StringBuilder builder = new StringBuilder();
TextView textView;
private void log(String text) {
    Log.d("LifeCycleTest", text);
    builder.append(text);
    builder.append('\n');
    textView.setText(builder.toString());
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    textView.setText(builder.toString());
    setContentView(textView);
    log("created");
}
@Override
protected void onResume() {
    super.onResume();
    log("resumed");
}
@Override
protected void onPause() {
        super.onPause();
        log("paused");
    if (isFinishing()) {
        log("finishing");
    }
}
}

And lastly the manifest file:

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

<uses-sdk android:minSdkVersion="15" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="Life Cycle Test"
        android:name=".LifeCycleTest"
        android:configChanges="keyboard|keyboardHidden|orientation" />
</application>

</manifest>

So can anyone help me figure out where my error is occuring? I already tried cleaning up the project but that did not resolve anything. Thanks so much for any possible ideas!

4
  • Make sure all the classes are in the correct packages. Commented Jul 30, 2012 at 4:52
  • I cut copied and paste what is present and it worked for me. Wait, I changed the minSDK to a Level 8 and removed the spaces here in LifeCycleTest cited in the Manifest. <activity android:label="LifeCycleTest" android:name=".LifeCycleTest" android:configChanges="keyboard|keyboardHidden|orientation" /> Commented Jul 30, 2012 at 5:04
  • Which spaces are you referring to? I just changed the minSDK level to 8 but the app is still not working for me! AHHHHHH, not fair! Commented Jul 30, 2012 at 5:08
  • Manifest <activity android:label="LifeCycleTest" android:name=".LifeCycleTest" android:configChanges="keyboard|keyboardHidden|orientation" /> </application> </manifest> Commented Jul 30, 2012 at 5:13

4 Answers 4

2

Do this to start the activity:

Intent intent = new Intent(this, LifeCycleTest.class);

There is no need to find the class using Class.forName

EDIT

The reason why it can't find the class is because you have misspelled the package.

It should be com.sandbox.basicsstarter. You omitted the d in sandbox.

Sign up to request clarification or add additional context in comments.

3 Comments

Well one, I would have to use a switch or something once multiple items in my listview lead to different activities correct? And secondly, I'd still like to learn what is causing the problem and why the code in a book I purchased does not work. Thanks for the reply, I will test that right now.
Look at my updated answer. It doesn't work because the package is misspelled.
ugh, thanks so much and I wish my first post was not a dumb question, but it is. Good catch!
0

The way you are starting your Activity is kind of a weird IMO, but if you insist on doing so, why don't you just use a Class[] tests instead?

1 Comment

Class.forName need parameter with full package name, as per documentation. docs.oracle.com/javase/1.4.2/docs/api/java/lang/…
0

Create intent like this

Intent intent = new Intent(AndroidBasicsStarterActivity .this, LifeCycleTest.class);
startActivity(intent);

1 Comment

I just tried that and I am still getting the same error. So that definitely means there is something wrong with my app not understanding LifeCycleTest is there I think?
0

If you insist on retrieving your class via the Class.forName method, try the following:

Class <?> clazz = Class.forName ( this.getPackageName() + "." + testName);

Intent intent = new Intent ( this , clazz );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.