启动应用程序时出现空白视图。我可以看到应该在XML预览中显示的内容,但是在启动应用程序时,它只是白色。我可以看到SurfaceView的轮廓,但是没有内容。
我是自学成才的,因此基础知识有限,并且想了解回复,因此非常感谢您提供简短的解释!
谢谢-Frenchie。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.Frenchie.SurfaceView.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dip"
android:id="@+id/surfaceView"/>
</LinearLayout>
主要活动
package com.Frenchie.SurfaceView;
import ...
public class MainActivity extends Activity {
com.Frenchie.Drawing.MySurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
}
}
MySurfaceView
package com.Frenchie.SurfaceView;
import ...
public class MySurfaceView extends SurfaceView implements Runnable {
private Bitmap bmp;
private SurfaceHolder holder;
public MySurfaceView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = holder.lockCanvas(null);
draw(c);
holder.unlockCanvasAndPost(c);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.player);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, (MeasureSpec.getSize(getMeasuredWidth()) - bmp.getWidth())/2, (MeasureSpec.getSize(getMeasuredHeight()) - bmp.getHeight())/2, null);
}
@Override
public void run() {
//TODO movement here when display is working
}
}
Logcat
11-27 16:20:43.437 24092-24092/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-27 16:20:43.450 24092-24092/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-27 16:20:43.804 24092-24092/com.Frenchie.SurfaceView I/InstantRun: starting instant run server: is main process
11-27 16:20:43.948 24092-24110/com.Frenchie.SurfaceView D/OpenGLRenderer: HWUI GL Pipeline
[ 11-27 16:20:44.001 24092:24110 D/ ]
HostConnection::get() New Host Connection established 0xa4329740, tid 24110
[ 11-27 16:20:44.005 24092:24110 W/ ]
Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1
11-27 16:20:44.011 24092-24110/com.Frenchie.SurfaceView I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
11-27 16:20:44.011 24092-24110/com.Frenchie.SurfaceView I/OpenGLRenderer: Initialized EGL, version 1.4
11-27 16:20:44.011 24092-24110/com.Frenchie.SurfaceView D/OpenGLRenderer: Swap behavior 1
11-27 16:20:44.013 24092-24110/com.Frenchie.SurfaceView D/EGL_emulation: eglCreateContext: 0xa43328a0: maj 2 min 0 rcv 2
11-27 16:20:44.018 24092-24110/com.Frenchie.SurfaceView D/EGL_emulation: eglMakeCurrent: 0xa43328a0: ver 2 0 (tinfo 0xaeb325d0)
11-27 16:20:44.104 24092-24110/com.Frenchie.SurfaceView D/EGL_emulation: eglMakeCurrent: 0xa43328a0: ver 2 0 (tinfo 0xaeb325d0)
11-27 16:20:45.783 24092-24092/com.Frenchie.SurfaceView V/StudioProfiler: StudioProfilers agent attached.
11-27 16:20:45.828 24092-24158/com.Frenchie.SurfaceView V/StudioProfiler: Acquiring Application for Events
11-27 16:20:45.849 24092-24092/com.Frenchie.SurfaceView V/StudioProfiler: Transformed class: java/net/URL
11-27 16:20:45.851 24092-24092/com.Frenchie.SurfaceView W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
11-27 16:20:46.312 24092-24092/com.Frenchie.SurfaceView V/StudioProfiler: Memory control stream started.
11-27 16:20:46.826 24092-24167/com.Frenchie.SurfaceView V/StudioProfiler: Live memory tracking disabled.
11-27 16:20:46.828 24092-24167/com.Frenchie.SurfaceView V/StudioProfiler: Live memory tracking enabled.
11-27 16:20:46.828 24092-24167/com.Frenchie.SurfaceView V/StudioProfiler: JNIEnv not attached
11-27 16:20:47.012 24092-24167/com.Frenchie.SurfaceView V/StudioProfiler: Loaded classes: 5094
11-27 16:20:47.416 24092-24167/com.Frenchie.SurfaceView V/StudioProfiler: Tracking initialization took: 588509880ns
如果您需要更多信息,请随时与我们联系。
最佳答案
当我在S6而不是仿真设备上运行程序时,此问题已解决。不知道为什么,但在这种情况下,它已解决了该问题。
关于java - 启动应用程序时的空白 View (SurfaceView),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47511368/