Android 使用libgdx的livewallpaper崩溃
我写了一个LiveWallper,它从一个活动开始,该活动使用libgdx的opengl20。 第一次启动活动时没有问题,但第二次LiveWallper崩溃。有时使用Android 使用libgdx的livewallpaper崩溃,android,opengl-es-2.0,libgdx,live-wallpaper,Android,Opengl Es 2.0,Libgdx,Live Wallpaper,我写了一个LiveWallper,它从一个活动开始,该活动使用libgdx的opengl20。 第一次启动活动时没有问题,但第二次LiveWallper崩溃。有时使用shader.begin(),而不是每次。 当我没有从那时开始活动时,没有问题 AndroidManifest.xml 事故信息 我打赌您正在将MeshShaderTest实例存储在static字段中,或者将ShaderProgram(meshShader)存储在static字段中。不过,如果没有更多的代码就很难说了 当您快速重新启
shader.begin()
,而不是每次。
当我没有从那时开始活动时,没有问题
AndroidManifest.xml
事故信息
我打赌您正在将
MeshShaderTest
实例存储在static
字段中,或者将ShaderProgram
(meshShader
)存储在static
字段中。不过,如果没有更多的代码就很难说了
当您快速重新启动活动时,Android可以重用先前初始化的DalvikVM,这意味着静态
字段将不会重新初始化。然而,由于所有OpenGL上下文都被丢弃,任何引用Libgdx状态的对象都将过时
有关更多详细信息,请参见我通过在活动中添加“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>
android:configChanges=“键盘|键盘隐藏|方向|屏幕大小”>
你能发布你的着色器代码和初始化代码吗?这样就可以确定根本原因我发布了,但我找不到任何问题我能理解你所说的,但是MeshShaderTest是静态GDX.app中的ApplicationListener商店,所以我应该做什么。我认为我的问题更复杂。因为应用程序是wallpaer。当第二个应用程序开始渲染时,第一个应用程序渲染不会停止。它们不是直接进行的。
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, 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.
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);
<?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>