0

我写了一个从一个活动开始的动态壁纸,这个活动使用了libgdx的opengl20。当我第一次启动活动时没有问题,但第二次动态壁纸崩溃。有时与shader.begin(),而不是每次。当我不从那时开始活动时,没有问题。

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>

启动动态壁纸代码

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

崩溃信息

06-27 11:03:22.576: D/dalvikvm(7139): GC_CONCURRENT freed 246K, 13% free 11375K/12999K, paused 15ms+10ms,总共61ms
06-27 11:03:22.626: W/dalvikvm(7139): threadid=16: 线程以未捕获的异常退出 (group=0x40e4c300)
06-26 11:45:09.936:E/AndroidRuntime(19979):致命异常:GLThread 9758
06-26 11:45:09.936: E/AndroidRuntime(19979): java.lang.NullPointerException
06-26 11:45:09.936: E/AndroidRuntime(19979): 在 com.badlogic.gdx.graphics.glutils.ShaderProgram.begin(ShaderProgram.java:745)
06-26 11:45:09.936: E/AndroidRuntime(19979): 在 com.wall.wall.MeshShaderTest.render(MeshShaderTest.java:78)
06-26 11:45:09.936: E/AndroidRuntime(19979): 在 com.badlogic.gdx.backends.android.AndroidGraphicsLiveWallpaper.onDrawFrame(AndroidGraphicsLiveWallpaper.java:625)
06-26 11:45:09.936: E/AndroidRuntime(19979): 在 android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1546)
06-26 11:45:09.936: E/AndroidRuntime(19979): 在 android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1247)
06-26 11:45:09.941:电子/窗口(1782):林恩。即使屏幕关闭,也可以完成该功能。

着色器

我认为它没有问题。

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

2 回答 2

1

我敢打赌您将MeshShaderTest实例存储在一个static字段中,或者您将ShaderProgram( meshShader?) 存储在一个static. 但是,如果没有更多代码,很难说。

当你快速重启activity时,Android可以重用之前初始化的DalvikVM,这意味着static字段不会被重新初始化。然而,由于所有的 OpenGL 上下文都被丢弃了,任何引用 Libgdx 状态的对象都会过时。

有关更多详细信息,请参阅http://bitiotic.com/blog/2013/05/23/libgdx-and-android-application-lifecycle/

于 2013-06-26T15:40:25.580 回答
0

我通过在活动中添加“android:process=":com.me.process.main" 和在服务中添加 android:process=":com.me.process.sub" 来更改我的 AndroidManifest.xml。然后活动和服务不是在一个进程中。所以他们的记忆不会导致问题。但我仍然不知道如何在同一进程中解决问题。

<?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>
于 2013-06-27T14:14:53.730 回答