我基于增强现实概念创建了一个简单的应用程序。这是下面的java代码。
package com.kddi.satch.tutorialactivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import com.kddi.satch.ARViewer;
import com.kddi.satch.LoadScenarioStatus;
public abstract class TutorialActivity_simple extends Activity
{
protected abstract String getSampleScenarioName();
protected abstract String getSampleLogTag();
protected boolean _isInitializedCorrectly;
protected ARViewer _kddiComponent;
protected FrameLayout _frameLayout;
private static final int DIALOG_EXIT = 0;
private void resetMembers()
{
_isInitializedCorrectly = false;
_frameLayout = null;
_kddiComponent = null;
}
public void initComponent()
{
_isInitializedCorrectly = false;
_kddiComponent = new ARViewer(this);
// This FrameLayout must be empty (but initialized) when you pass it to the kddiComponent.initialize() method.
_frameLayout = new FrameLayout(this);
_kddiComponent.initialize(_frameLayout);
_isInitializedCorrectly = true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add null to AR Viewer Library compornent's reference.
resetMembers();
}
@Override
public void onRestart() {
super.onRestart();
}
@Override
public void onStart() {
super.onStart();
// Create AR Viewer Library compornent.
initComponent();
postInitComponent();
initContentView();
if (_isInitializedCorrectly) {
// Do authorize and madia is loaded.
// You must call loadScenario() method.
loadScenario();
}
}
@Override
public void onResume(){
super.onResume();
if (_isInitializedCorrectly) {
// GL context is recreated and media is reloaded.
_kddiComponent.onResume();
reservePlayScenario();
}
}
@Override
public void onPause() {
// When the activity is paused the GL context is destroyed, so all media is unloaded.
if (_isInitializedCorrectly) {
cancelReservePlayScenario();
if (_kddiComponent.checkLoadScenarioStatus() == LoadScenarioStatus.COMPLETE) {
_kddiComponent.pauseScenario();
}
_kddiComponent.onPause();
}
super.onPause();
}
@Override
public void onStop() {
releaseContentView();
// Destroy AR Viewer Library Objects.
if (_isInitializedCorrectly)
{
_kddiComponent.terminate();
_kddiComponent = null;
_frameLayout = null;
}
super.onStop();
}
@Override
public void onDestroy() {
// Destroy AR Viewer Library Objects.
if (_isInitializedCorrectly)
{
_frameLayout = null;
_kddiComponent = null;
}
super.onDestroy();
resetMembers(); // forced clean
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg)
{
switch(keyCode){
case android.view.KeyEvent.KEYCODE_BACK :
showDialog( DIALOG_EXIT );
return true;
}
return false;
}
public void postInitComponent()
{
// override this if you need to do some special handling on the component after standard initialization
if (_isInitializedCorrectly) {
_kddiComponent.activateAutoFocusOnDownEvent(true);
}
}
public void initContentView()
{
// override this if you need to do some special handling on the component after standard initialization
if (_isInitializedCorrectly) {
// you'll probably use some other UI object as the content view that itself will embed the component's frame layout -- here you can change all this
// by default, the frame layout containing DFusion will be the activity content view
setContentView(_frameLayout);
}
}
public void releaseContentView()
{
// override this if you need to do some special handling on the component after standard initialization
if (_isInitializedCorrectly) {
// do here the release of the your UI instances (if customized)
}
}
public void loadScenario()
{
ApplicationInfo appInfo = null;
PackageManager packMgmr = getApplicationContext().getPackageManager();
try {
appInfo = packMgmr.getApplicationInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Unable to locate assets, aborting...");
}
String dpdfile = appInfo.sourceDir + getSampleScenarioName();
_kddiComponent.loadScenario(dpdfile);
}
// Set polling rate for loading the media.
private final int REPEAT_INTERVAL = 100;
private Handler handler = new Handler();
private Runnable runnable = null;
private void reservePlayScenario()
{
if (runnable == null)
{
runnable = new Runnable()
{
@Override
public void run()
{
LoadScenarioStatus status = _kddiComponent.checkLoadScenarioStatus();
if (status == LoadScenarioStatus.CANCEL)
{
// cancel(appli suspend)
}
else if (status == LoadScenarioStatus.COMPLETE)
{
// Ready to play scenario
_frameLayout.setVisibility(View.VISIBLE);
_kddiComponent.playScenario();
}
else if (
status == LoadScenarioStatus.ERROR_NETWORK_UNUSABLE ||
// faild to load a media becase of no network connection.
status == LoadScenarioStatus.ERROR_NETWORK ||
// faild to load a media becase of network error.
status == LoadScenarioStatus.ERROR_SOFTWAREKEY ||
// faild to load a media becase software key has not be found on server.
status == LoadScenarioStatus.ERROR_CONTENT_STOPPED ||
// faild to load a media becase content has stopped.
status == LoadScenarioStatus.ERROR_SERVER ||
// faild to load a media becase of server error.
status == LoadScenarioStatus.ERROR_ETC
// faild to load a media becase of another error.
)
{
// error
}
else
{
handler.postDelayed(this, REPEAT_INTERVAL);
}
}
};
handler.postDelayed(runnable, REPEAT_INTERVAL);
}
}
private void cancelReservePlayScenario()
{
if (handler != null && runnable != null)
{
handler.removeCallbacks(runnable);
runnable = null;
}
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_EXIT:
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Really want to quit the sample?"
)
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}});
dialog = builder.create();
break;
}
default:
dialog = null;
}
return dialog;
}
}
//-- end of file --
应用程序的 manifest.xml 文件如下所示
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" package="com.kddi.satch.tutorial">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:debuggable="false">
<!-- configChanges setting -->
<activity android:name=".Tutorial"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboard|keyboardHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- define the resolution -->
<supports-screens
android:anyDensity="false"
android:normalScreens="true"
android:largeScreens="true"
android:smallScreens="true"
android:resizeable="true">
</supports-screens>
<!-- permit camera usage if authorized -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- define Autofocus -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk android:minSdkVersion="8" />
现在,每当我在模拟器上运行应用程序时,第一次加载时应用程序强制关闭,并出现以下错误。
06-06 22:55:00.157: A /libc(19290): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1)
06-06 22:55:00.173: E /ti.dfusionmobile.tiComponent(19290): ON SURFACE CREATED
任何关于我出错的地方和地方的指导都是可观的。提前致谢。