我正在尝试渲染一个渲染 droidar 对象的 Google Cardboard 应用程序,但现在我正在尝试使用官方教程(https://developers.google.com/cardboard/android/get-started)显示一个简单的 cardboardview但我有一个例外:
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.google.vrtoolkit.cardboard.CardboardView
而且我找不到解决方案,它开始让我发疯......你能帮帮我吗?这是我的代码:
我的 Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lp.s3im.cardboarddemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="com.google.intent.category.CARDBOARD" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动 :
package lp.s3im.cardboarddemo;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import com.google.vrtoolkit.cardboard.CardboardActivity;
import com.google.vrtoolkit.cardboard.CardboardView;
import com.google.vrtoolkit.cardboard.Eye;
import com.google.vrtoolkit.cardboard.HeadTransform;
import com.google.vrtoolkit.cardboard.Viewport;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends CardboardActivity implements
CardboardView.StereoRenderer {
private static final String TAG = "MainActivity";
private static final float Z_NEAR = 0.1f;
private static final float Z_FAR = 100.0f;
private static final float CAMERA_Z = 0.01f;
private static final float TIME_DELTA = 0.3f;
private static final float YAW_LIMIT = 0.12f;
private static final float PITCH_LIMIT = 0.12f;
private static final int COORDS_PER_VERTEX = 3;
// We keep the light always position just above the user.
private static final float[] LIGHT_POS_IN_WORLD_SPACE = new float[] { 0.0f,
2.0f, 0.0f, 1.0f };
private final float[] lightPosInEyeSpace = new float[4];
private FloatBuffer floorVertices;
private FloatBuffer floorColors;
private FloatBuffer floorNormals;
private FloatBuffer cubeVertices;
private FloatBuffer cubeColors;
private FloatBuffer cubeFoundColors;
private FloatBuffer cubeNormals;
private int cubeProgram;
private int floorProgram;
private int cubePositionParam;
private int cubeNormalParam;
private int cubeColorParam;
private int cubeModelParam;
private int cubeModelViewParam;
private int cubeModelViewProjectionParam;
private int cubeLightPosParam;
private int floorPositionParam;
private int floorNormalParam;
private int floorColorParam;
private int floorModelParam;
private int floorModelViewParam;
private int floorModelViewProjectionParam;
private int floorLightPosParam;
private float[] modelCube;
private float[] camera;
private float[] view;
private float[] headView;
private float[] modelViewProjection;
private float[] modelView;
private float[] modelFloor;
private int score = 0;
private float objectDistance = 12f;
private float floorDepth = 20f;
private Vibrator vibrator;
// private CardboardOverlayView overlayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboard_view);
// Associate a CardboardView.StereoRenderer with cardboardView.
cardboardView.setRenderer(this);
// Associate the cardboardView with this activity.
setCardboardView(cardboardView);
// Initialize other objects here.
modelCube = new float[16];
camera = new float[16];
view = new float[16];
modelViewProjection = new float[16];
modelView = new float[16];
modelFloor = new float[16];
headView = new float[16];
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawEye(Eye arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFinishFrame(Viewport arg0) {
// TODO Auto-generated method stub
}
@Override
public void onNewFrame(HeadTransform arg0) {
// TODO Auto-generated method stub
arg0.getHeadView(headView, 0);
}
@Override
public void onRendererShutdown() {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceChanged(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceCreated(EGLConfig arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="lp.s3im.cardboarddemo.MainActivity" >
<com.google.vrtoolkit.cardboard.CardboardView
android:id="@+id/cardboard_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
编辑:使用以下代码,我可以使用 starpreview 方法显示相机,但我不能使用 andAR 库(https://code.google.com/p/andar/)的预览,它会出现黑屏. 我不明白是不是因为它超载了。
package edu.dhbw.andar.sample;
import java.io.IOException;
import java.util.List;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import edu.dhbw.andar.ARToolkit;
import edu.dhbw.andar.AndARActivity;
import edu.dhbw.andar.exceptions.AndARException;
/**
* Example of an application that makes use of the AndAR toolkit.
*
* @author Tobi
*
*/
public class CustomActivity extends AndARActivity implements SurfaceHolder.Callback {
CustomObject someObject;
ARToolkit artoolkit;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
CustomRenderer renderer = new CustomRenderer();// optional, may be set
// to null
super.setNonARRenderer(renderer);// or might be omited
try {
// register a object for each marker type
artoolkit = super.getArtoolkit();
someObject = new CustomObject("test", "patt.hiro", 80.0,
new double[] { 0, 0 });
artoolkit.registerARObject(someObject);
someObject = new CustomObject("test", "android.patt", 80.0,
new double[] { 0, 0 });
artoolkit.registerARObject(someObject);
someObject = new CustomObject("test", "barcode.patt", 80.0,
new double[] { 0, 0 });
artoolkit.registerARObject(someObject);
} catch (AndARException ex) {
// handle the exception, that means: show the user what happened
System.out.println("");
}
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
startCamera(holder, width, height);
}
@Override
public SurfaceView getSurfaceView() {
// TODO Auto-generated method stub
return super.getSurfaceView();
}
@Override
public void startPreview() {
// TODO Auto-generated method stub
super.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.surfaceDestroyed(holder);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
}
}
/**
* Inform the user about exceptions that occurred in background threads.
* This exception is rather severe and can not be recovered from. TODO
* Inform the user and shut down the application.
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("AndAR EXCEPTION", ex.getMessage());
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
// case R.id.settings:
// break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void startCamera(SurfaceHolder sh, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(sh);
} catch (Exception e) {
}
camera.startPreview();
}
private void stopCamera() {
//camera.removeCallback(this);
camera.stopPreview();
camera.release();
}
}