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!
-
Make sure all the classes are in the correct packages.Will Richardson– Will Richardson2012年07月30日 04:52:36 +00:00Commented 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" />KaSiris– KaSiris2012年07月30日 05:04:56 +00:00Commented 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!Alex Harris– Alex Harris2012年07月30日 05:08:54 +00:00Commented Jul 30, 2012 at 5:08
-
Manifest <activity android:label="LifeCycleTest" android:name=".LifeCycleTest" android:configChanges="keyboard|keyboardHidden|orientation" /> </application> </manifest>KaSiris– KaSiris2012年07月30日 05:13:34 +00:00Commented Jul 30, 2012 at 5:13
4 Answers 4
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.
3 Comments
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
Create intent like this
Intent intent = new Intent(AndroidBasicsStarterActivity .this, LifeCycleTest.class);
startActivity(intent);
1 Comment
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
Explore related questions
See similar questions with these tags.