hstcscolor
hstcscolor

Reputation: 13

livewallpaper crash which use libgdx

I write a livewallpaper which start from an activity, the activity uses libgdx's opengl20. When I start the activity for the first time there is no problem, but the second time the livewallpaper crashes. Sometimes with shader.begin(), not everytime. when I not start from then activity there no problem.

AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".LivewallpaperSettings" 
              android:label="Livewallpaper Settings"
              android:parentActivityName="com.me.mygdxgame.MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

    <service android:name=".LiveWallpaper"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_WALLPAPER">
        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService" />
        </intent-filter>
        <meta-data android:name="android.service.wallpaper"
            android:resource="@xml/livewallpaper" />
    </service>

</application>

Start livewallpaper code

            Intent i = new Intent();
            if (Build.VERSION.SDK_INT > 15)
            {
                i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
                String pkg = LiveWallpaper.class.getPackage().getName();
                String cls = LiveWallpaper.class.getCanonicalName();
                i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls));

            }
            else
            {
                i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
            }
            startActivityForResult(i, 0);

The crash info

06-27 11:03:22.576: D/dalvikvm(7139): GC_CONCURRENT freed 246K, 13% free 11375K/12999K, paused 15ms+10ms, total 61ms
06-27 11:03:22.626: W/dalvikvm(7139): threadid=16: thread exiting with uncaught exception (group=0x40e4c300)
06-26 11:45:09.936: E/AndroidRuntime(19979): FATAL EXCEPTION: GLThread 9758
06-26 11:45:09.936: E/AndroidRuntime(19979): java.lang.NullPointerException
06-26 11:45:09.936: E/AndroidRuntime(19979):    at com.badlogic.gdx.graphics.glutils.ShaderProgram.begin(ShaderProgram.java:745)
06-26 11:45:09.936: E/AndroidRuntime(19979):    at com.wall.wall.MeshShaderTest.render(MeshShaderTest.java:78)
06-26 11:45:09.936: E/AndroidRuntime(19979):    at com.badlogic.gdx.backends.android.AndroidGraphicsLiveWallpaper.onDrawFrame(AndroidGraphicsLiveWallpaper.java:625)
06-26 11:45:09.936: E/AndroidRuntime(19979):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1546)
06-26 11:45:09.936: E/AndroidRuntime(19979):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1247)
06-26 11:45:09.941: E/Window(1782): Lynn . finish the function even if the screen is off. 

The shader

I think it has no problems.

    vertexShader = "attribute vec4 a_Position;    \n"
                    + "attribute vec2 a_texCoords;    \n"
                    + "uniform mat4 u_mvp_matrix;\n"
                    + "varying vec2 v_texCoords;     \n"
                    + "void main()                   \n"
                    + "{                             \n"
            //      + "   gl_Position = a_Position;  \n"
                    + " gl_Position = u_mvp_matrix * a_Position;\n"
                    + "   v_texCoords = a_texCoords;  \n"
                    + "}                             \n";

// this one tells it what goes in between the points (i.e
// colour/texture)
  fragmentShader = "#ifdef GL_ES                \n"
                      + "precision mediump float;    \n"
                      + "#endif                      \n"
                      + "varying vec2 v_texCoords;   \n"
                      + "uniform sampler2D u_texture;\n"
                      + "void main()                 \n"
                      + "{                           \n"
                     // + "  gl_FragColor = vec4(1.0,0.0,0.0,1.0);  \n"
                      + "  gl_FragColor =  texture2D(u_texture, v_texCoords);   \n"
                      + "}";
    meshShader = new ShaderProgram(vertexShader, fragmentShader);

Upvotes: 0

Views: 696

Answers (2)

hstcscolor
hstcscolor

Reputation: 13

I alter my AndroidManifest.xml by adding "android:process=":com.me.process.main" in activity and android:process=":com.me.process.sub" in service. then the activity and the service not in a process.so their memory would not lead to the problem. But I still don't know how to solve the prolem in same process.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.mygdxgame"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:process=":com.me.process.main"   >
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".LiveWallpaper"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_WALLPAPER"
             android:process=":com.me.process.sub">
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/livewallpaper" />
        </service>

    </application>

</manifest>

Upvotes: 0

P.T.
P.T.

Reputation: 25177

I'll bet you are storing your MeshShaderTest instance in a static field, or you're storing the ShaderProgram (meshShader?) in a static. Its hard to say without more of the code, though.

When you restart the activity quickly, Android can reuse the previously initialized DalvikVM, which means static fields will not get re-initialized. However, since all the OpenGL context was thrown away, any object referencing Libgdx state will have gone stale.

For more details see http://bitiotic.com/blog/2013/05/23/libgdx-and-android-application-lifecycle/

Upvotes: 1

Related Questions