我写了一个从一个活动开始的动态壁纸,这个活动使用了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);