android-studio - 使用 Android 插件时 Unity3d 应用程序崩溃

标签 android-studio google-play-services google-fit unity3d-5 android-unity-plugin

我关注 this tutorial制作一个计步器,它作为一个 android 项目运行良好,但是当我将 android 项目作为一个库并想在 unity3d 中使用它时,它崩溃并给我一个错误 class not found: exception我的统一代码如下:

void Start () 
{
    #if UNITY_ANDROID
    AndroidJNI.AttachCurrentThread();
    androidClass = new AndroidJavaClass("com.test.testapp.MainActivity");
    #endif
}
#if UNITY_ANDROID
public void checkMyIntOnJava(string message){
    if (message == "READY") {
        int myInt = androidClass.CallStatic<int>("getMyInt");
        guiText.text = "My Int: " + myInt;
    }
}

我的android代码如下:
public class MainActivity extends UnityPlayerActivity
implements OnDataPointListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener
{
public static void callbackToUnityMethod(int num, String gameObject,String methodName){
    _myInt = num;
    Log.e("GoogleFit", "in callbackToUnityMethod");
   UnityPlayer.UnitySendMessage(gameObject, methodName, "READY");
}
}

制作了一个android jar后,我将它保存在unity3d的插件文件夹中。
我错过了什么吗?

我的 Android list 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.xxx.testapp" android:versionCode="1" 
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:label="@string/app_name">
    <activity android:name="com.xxx.testapp.MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.xxx.testapp.UnityPlayerActivity"  
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" 
    android:label="@string/app_name" android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
</application>
</manifest>

最佳答案

这是我会尝试的两件事。

首先在你AndroidManifest.xml中更改以下行以便包范围匹配 android:name属性(property)。

<activity android:name="com.xxx.testapp.MainActivity"
         android:label="@string/app_name">


<activity android:name="com.test.testapp.MainActivity"
         android:label="@string/app_name">

另一个潜在的问题是您的类没有被编译,因为它缺少父类和接口(interface)中抽象方法的具体实现。要解决这个问题,请将这些实现添加到 MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onDataPoint(DataPoint dataPoint) {

}

关于android-studio - 使用 Android 插件时 Unity3d 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856460/

相关文章:

安卓 Google Fit onConnectionFailed() SIGN_IN_REQUIRED

java - Android Studio,缺少android.support.annotation包

无法安装 Android Play Services Leaderboard (BaseGameUtils)

android - 如何用 gradle 解决所有 google play-services 依赖?

android - 不可作弊的 Google Fit 计步器

android - 使用 Google Fit 的 Android 应用程序中的 OAuth 同意屏幕空白

android - Action 按钮没有覆盖整个工具栏

android - 如何从Android中的日历中提取日期?

android - 如何使用 lint.xml 从 Android Studio 中的 lint 中排除一些未使用的图像?

android - 使用 Google Play 服务检查 GPS 状态