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