user2019170
user2019170

Reputation: 17

ClassNotFoundException in android program

I'm studyng from the book beginning android games opengl es for android. I recreated an application to test some of the book's notions, based on his previous examples: here is the code

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();
        }
    }
}

As you could see, it creates a list of others activities with the list of names reported in the tests array. Now: here there are the codes of the 2 activities:

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);
        }
    }
}

and

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() {
        }
    }
}

also, there is the XML manifest:

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

Now: when i start the application, evertyhing seems to be fine: it appaers a list with the 2 activities; but when i try to click one, a message appaers on the 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

There must be an error on the nomenclature of the packages or classes, but i can't see anything. so where is the problem? Sometimes ago I created a similiar (radically identical) application previously to test other things and i never had errors, so...?

Upvotes: 1

Views: 829

Answers (2)

Atropo
Atropo

Reputation: 12541

Check for your package declaration:

In your classes you have:

package com.badlogic.androidgames.glbasics;

While in the error you posted you need something like:

com.badlogic.androidgames.framework.glbasics.GLGameTest

Upvotes: 1

Grambot
Grambot

Reputation: 4524

Exception states you're searching for com.badlogic.androidgames.framework.glbasics

Note the "framework"

This is how you're defining your class, where the exception originates...

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

But all your classes are....

package com.badlogic.androidgames.glbasics;

No "framework"

Upvotes: 1

Related Questions