找不到方法view.View.getDisplay()

时间:2014-05-10 19:10:03

标签: android runtime-error google-client

我相信有人发布了类似的错误,但通常我在实施Google Play游戏服务API时遇到了问题。

在onCreate()中创建Google客户端对象时出现此日志错误:

05-10 15:06:05.490: E/Trace(23953): error opening trace file: No such file or directory (2)
05-10 15:06:05.490: D/ActivityThread(23953): setTargetHeapUtilization:0.25
05-10 15:06:05.490: D/ActivityThread(23953): setTargetHeapIdealFree:8388608
05-10 15:06:05.490: D/ActivityThread(23953): setTargetHeapConcurrentStart:2097152
05-10 15:06:05.930: I/dalvikvm(23953): Could not find method android.view.View.getDisplay, referenced from method com.google.android.gms.internal.gd$b.g
05-10 15:06:05.930: W/dalvikvm(23953): VFY: unable to resolve virtual method 3404: Landroid/view/View;.getDisplay ()Landroid/view/Display;
05-10 15:06:05.930: D/dalvikvm(23953): VFY: replacing opcode 0x6e at 0x0009
05-10 15:06:05.930: D/AndroidRuntime(23953): Shutting down VM
05-10 15:06:05.930: W/dalvikvm(23953): threadid=1: thread exiting with uncaught exception (group=0x4189a438)
05-10 15:06:05.940: E/AndroidRuntime(23953): FATAL EXCEPTION: main
05-10 15:06:05.940: E/AndroidRuntime(23953): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexkidd.bitvsbit/com.alexkidd.bitgame.BitGame}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1231)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.os.Looper.loop(Looper.java:137)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread.main(ActivityThread.java:5021)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at java.lang.reflect.Method.invokeNative(Native Method)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at java.lang.reflect.Method.invoke(Method.java:511)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at dalvik.system.NativeStart.main(Native Method)
05-10 15:06:05.940: E/AndroidRuntime(23953): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-10 15:06:05.940: E/AndroidRuntime(23953):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:215)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.Activity.requestWindowFeature(Activity.java:3263)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at com.alexkidd.implement.AndroidGame.onCreate(AndroidGame.java:70)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.Activity.performCreate(Activity.java:5058)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-10 15:06:05.940: E/AndroidRuntime(23953):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-10 15:06:05.940: E/AndroidRuntime(23953):    ... 11 more

我下载了最新版本的Google Client API,我正在使用Eclipse。

我的主要活动中的相关Google客户端代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.signin);
    this.mGoogleClient = new GoogleApiClient.Builder(this, this, this)
    .addApi(Games.API)
    .addScope(Games.SCOPE_GAMES)
    .setViewForPopups(this.findViewById(R.id.sign_in_button))
    .setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
    .build();

     mResolvingError = savedInstanceState != null
                && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 480 : 800;
    int frameBufferHeight = isPortrait ? 800 : 480;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);
    display = getWindowManager().getDefaultDisplay();
    checkDimens();
    // Scaling. Good 2 Go.
    float scaleX = (float) frameBufferWidth / width;
    float scaleY = (float) frameBufferHeight / height;

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen();
    lastScreen = screen;
    setContentView(renderView);
}

2 个答案:

答案 0 :(得分:1)

在API级别17中添加了

View.getDisplay(),即Android 4.2。您可能正在使用以前版本的设备上执行代码。您或某些库可能仅在4.2+的设备中调用getDisplay(),但设备VM在某些代码路径上看到调用,并且它不知道该方法,因此它将替换它。 / p>

但是,由于getDisplay(),您的应用不会崩溃,但是因为在第70行,您在调用Activity.requestWindowFeature后呼叫setContentView。至少这就是StackTrace所说的。

答案 1 :(得分:0)

通过堆栈跟踪,您在设置内容this.setContentView(R.layout.signin)后尝试更改窗口功能时,应在设置内容之前调用requestWindowFeature