简体   繁体   English

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

[英]Unity3d Application crashes when use Android plugin

I am following this tutorial to make a step counter and it works well as an android project but when I make the android project a library and want to use it in unity3d, it is crashing and giving me an error class not found: exception My unity code is as follows: 我正在按照本教程做一个计数器,它可以作为android项目很好地工作,但是当我将android项目做成一个库并想在unity3d中使用它时,它崩溃了,并且给了我一个错误class not found: exception My unity代码如下:

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;
    }
}

and my android code is as follows: 和我的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");
}
}

After making an android jar, I keep it in the plugin folder in unity3d. 制作完一个Android jar之后,我将其保存在unity3d的plugin文件夹中。 Did I miss anything? 我有想念吗?

My Android Manifest file is as following: 我的Android清单文件如下:

<?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>

Here's the two things I would try. 这是我要尝试的两件事。

First change the following line in you AndroidManifest.xml so that the package scoping matches on the android:name property. 首先,在AndroidManifest.xml更改以下行,以使包作用域与android:name属性匹配。

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

to

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

The other potential problem is that your class isn't being compiled because it is missing concrete implementations of the abstract methods in the parents classes and interfaces. 另一个潜在的问题是您的类没有被编译,因为它缺少父类和接口中抽象方法的具体实现。 To fix that add these implementations to MainActivity 要解决此问题,请将这些实现添加到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) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM