1

我正在从这本书开始学习 android 游戏 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();
        }
    }
}

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

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 清单:

<?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 个活动的列表;但是当我尝试单击一个时,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

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

4

2 回答 2

1

您正在搜索的异常状态com.badlogic.androidgames.framework.glbasics

注意“框架”

这就是你定义你的类的方式,异常的来源......

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

但是你所有的课程都是......

package com.badlogic.androidgames.glbasics;

没有“框架”

于 2013-02-01T16:25:37.797 回答
1

检查您的包裹声明:

在您的课程中,您有:

package com.badlogic.androidgames.glbasics;

在您发布的错误中,您需要以下内容:

com.badlogic.androidgames.framework.glbasics.GLGameTest
于 2013-02-01T16:28:42.087 回答