简体   繁体   中英

Android: java.lang.ClassNotFoundException

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!

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.

你开始你的Activity是一种奇怪的IMO,但如果你坚持这样做,为什么不只是使用Class[] tests呢?

Create intent like this

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

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM