gpt4 book ai didi

java - android程序中的ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-29 21:50:09 25 4
gpt4 key购买 nike

我正在从 android games opengl es for android 开始学习。我根据他之前的示例重新创建了一个应用程序来测试本书的一些概念:这是代码

package com.badlogic.androidgames.glbasics;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class OpenGLBasicsStarter extends ListActivity {
String tests[] = { "GLSurfaceViewTest", "GLGameTest" };

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tests));
}

@Override
protected void onListItemClick(ListView list, View view, int position,
long id) {
super.onListItemClick(list, view, position, id);
String testName = tests[position];
try {
Class clazz = Class
.forName("com.badlogic.androidgames.framework.glbasics." + testName);
Intent intent = new Intent(this, clazz);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

如您所见,它使用测试数组中报告的名称列表创建了其他 Activity 的列表。现在:这里有 2 个 Activity 的代码:

package com.badlogic.androidgames.glbasics;

import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class GLSurfaceViewTest extends Activity {
GLSurfaceView glView;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(new SimpleRenderer());
setContentView(glView);
}

@Override
public void onResume() {
super.onPause();
glView.onResume();
}

@Override
public void onPause() {
super.onPause();
glView.onPause();
}

static class SimpleRenderer implements Renderer {
Random rand = new Random(); //crea i numeri random

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.d("GLSurfaceViewTest", "surface created");
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d("GLSurfaceViewTest", "surface changed: " + width + "x"
+ "height");
}

@Override
public void onDrawFrame(GL10 gl) {
gl.glClearColor(rand.nextFloat(), rand.nextFloat(),
rand.nextFloat(), 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
}
}

package com.badlogic.androidgames.glbasics;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLGraphics;



public class GLGameTest extends GLGame {
@Override
public Screen getStartScreen() {
return new TestScreen(this);
}

class TestScreen extends Screen {
GLGraphics glGraphics;
Random rand = new Random();

public TestScreen(Game game) {
super(game);
glGraphics = ((GLGame) game).getGLGraphics();
}

@Override
public void present(float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glClearColor(rand.nextFloat(), rand.nextFloat(),
rand.nextFloat(), 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}

@Override
public void update(float deltaTime) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {
}
}
}

还有 XML list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.badlogic.androidgames.glbasics"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="preferExternal" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="9" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="GL"
android:debuggable="true"
android:theme="@style/AppTheme" >
<activity
android:label="GL Surface View Test"
android:name=".GLSurfaceViewTest"
android:configChanges="keyboard|keyboardHidden|orientation" />
<activity
android:label="GL Game Test"
android:name=".GLGameTest"
android:configChanges="keyboard|keyboardHidden|orientation" />
<activity
android:name=".OpenGLBasicsStarter"
android:label="OpenGL Basics Starter"
android:configChanges="keyboard|keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>

现在:当我启动应用程序时,一切似乎都很好:它出现了一个包含 2 个 Activity 的列表;但是当我尝试单击一个时,logcat 上出现一条消息:

02-01 16:10:20.540: W/System.err(394): java.lang.ClassNotFoundException: com.badlogic.androidgames.framework.glbasics.GLGameTest
02-01 16:10:20.550: W/System.err(394): at java.lang.Class.classForName(Native Method)
02-01 16:10:20.550: W/System.err(394): at java.lang.Class.forName(Class.java:234)
02-01 16:10:20.550: W/System.err(394): at java.lang.Class.forName(Class.java:181)
02-01 16:10:20.550: W/System.err(394): at com.badlogic.androidgames.glbasics.OpenGLBasicsStarter.onListItemClick(OpenGLBasicsStarter.java:26)
02-01 16:10:20.550: W/System.err(394): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
02-01 16:10:20.550: W/System.err(394): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-01 16:10:20.550: W/System.err(394): at android.widget.ListView.performItemClick(ListView.java:3513)
02-01 16:10:20.550: W/System.err(394): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
02-01 16:10:20.550: W/System.err(394): at android.os.Handler.handleCallback(Handler.java:587)
02-01 16:10:20.550: W/System.err(394): at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 16:10:20.550: W/System.err(394): at android.os.Looper.loop(Looper.java:123)
02-01 16:10:20.550: W/System.err(394): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-01 16:10:20.550: W/System.err(394): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 16:10:20.550: W/System.err(394): at java.lang.reflect.Method.invoke(Method.java:507)
02-01 16:10:20.550: W/System.err(394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-01 16:10:20.550: W/System.err(394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-01 16:10:20.550: W/System.err(394): at dalvik.system.NativeStart.main(Native Method)
02-01 16:10:20.560: W/System.err(394): Caused by: java.lang.NoClassDefFoundError: com.badlogic.androidgames.framework.glbasics.GLGameTest
02-01 16:10:20.560: W/System.err(394): ... 17 more
02-01 16:10:20.560: W/System.err(394): Caused by: java.lang.ClassNotFoundException: com.badlogic.androidgames.framework.glbasics.GLGameTest in loader dalvik.system.PathClassLoader[/mnt/asec/com.badlogic.androidgames.glbasics-1/pkg.apk]
02-01 16:10:20.560: W/System.err(394): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-01 16:10:20.560: W/System.err(394): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-01 16:10:20.560: W/System.err(394): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-01 16:10:20.560: W/System.err(394): ... 17 more

一定是包或类的命名有误,但我什么也看不到。那么问题在哪里呢?有时我之前创建了一个类似的(完全相同的)应用程序来测试其他东西,但我从来没有出错,所以......?

最佳答案

异常表明您正在搜索 com.badlogic.androidgames.framework.glbasics

注意“框架”

这就是您定义类的方式,异常起源于此...

Class clazz = Class.forName("com.badlogic.androidgames.framework.glbasics." + testName);

但是你所有的类都是......

package com.badlogic.androidgames.glbasics;

没有“框架”

关于java - android程序中的ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14650511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com