简体   繁体   English

Android:java.lang.ClassNotFoundException

[英]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. 我对Android编程很新,我正在从我购买的一本书中找到一个例子,但他们向我展示如何制作的示例应用程序正在提出它中的错误。 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: 我的代码实际上是3个单独的文件,第一个是调用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: 那么LifeCycleTest类:

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 无需使用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 . 它应该是com.sandbox.basicsstarter You omitted the d in sandbox. 您在沙箱中省略了d

你开始你的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.forName方法检索您的类,请尝试以下操作:

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

Intent intent = new Intent ( this , clazz );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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