我写了一个从活动开始的livewallpaper,活动使用了libgdx的opengl20。
当我第一次开始活动时没有问题,但第二次活壁纸崩溃了。有时使用shader.begin()
,而不是每次都使用<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);
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);
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.
我认为没有问题。
{{1}}
答案 0 :(得分:1)
我敢打赌,您将MeshShaderTest
个实例存储在static
字段中,或者您将ShaderProgram
(meshShader
?)存储在{{1}中}}。但是,如果没有更多的代码,很难说。
当您快速重新启动活动时,Android可以重复使用先前初始化的DalvikVM,这意味着static
字段将不会重新初始化。但是,由于抛弃了所有OpenGL上下文,任何引用Libgdx状态的对象都将过时。
有关详细信息,请参阅http://bitiotic.com/blog/2013/05/23/libgdx-and-android-application-lifecycle/
答案 1 :(得分: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>