0

我对 android 编程相当陌生,我正在研究我购买的书中的一个示例,但是他们向我展示如何制作的示例应用程序中出现了错误。到目前为止它非常简单,但到目前为止我收到以下错误:

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)

我的代码实际上是在 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();
    }
}
}

然后是 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");
    }
}
}

最后是清单文件:

<?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>

那么谁能帮我弄清楚我的错误发生在哪里?我已经尝试清理项目,但这并没有解决任何问题。非常感谢任何可能的想法!

4

4 回答 4

2

执行此操作以启动活动:

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

无需使用查找类Class.forName

编辑

找不到类的原因是你拼错了包。

应该是com.sandbox.basicsstarter。您省略了d沙箱中的内容。

于 2012-07-30T04:54:06.313 回答
0

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

于 2012-07-30T04:55:17.493 回答
0

像这样创建意图

Intent intent = new Intent(AndroidBasicsStarterActivity .this, LifeCycleTest.class);
startActivity(intent);
于 2012-07-30T04:55:19.837 回答
0

如果您坚持通过 Class.forName 方法检索您的类,请尝试以下操作:

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

Intent intent = new Intent ( this , clazz );
于 2012-07-30T08:54:58.460 回答