Unity Android:没有名称的非静态方法

时间:2017-08-22 02:43:57

标签: android unity3d

我正在制作一个统一的Android插件演示,当我点击按钮时,它会启动另一个应用程序。我可以构建一个apk并将其安装到我的设备上。但是,当我点击按钮时,没有任何事情发生,但是语录:Unity:AndroidJavaException:java.lang.NoSuchMethodError:没有名称='shareToWX'的非静态方法

这是Android代码:

public class WXEntryActivity extends UnityPlayerActivity implements IWXAPIEventHandler {
private static final String APP_ID="";
private static final String APP_SECRET="";
private static IWXAPI api;
private static final int WXSceneSession=0;
private static final int WXSceneTimeLine=1;
private static final int WXSceneFavorite=2;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    api= WXAPIFactory.createWXAPI(this,APP_ID,true);
    api.registerApp(APP_ID);
    api.handleIntent(getIntent(),this);
}


public void shareToWX(String text,int scene){
    WXWebpageObject webpage=new WXWebpageObject();
    webpage.webpageUrl="www.baidu.com";

    WXMediaMessage msg =new WXMediaMessage(webpage);
    msg.title="网页标题";
    msg.description="描述";
    Bitmap thumb=BitmapFactory.decodeResource(getResources(), R.drawable.test);
    //msg.thumbData=

    SendMessageToWX.Req req=new SendMessageToWX.Req();
    req.transaction=buildTransaction("webpage");
    req.message=msg;
    switch (scene){
        case WXSceneFavorite:
            req.scene=SendMessageToWX.Req.WXSceneFavorite;
            break;
        case WXSceneTimeLine:
            req.scene=SendMessageToWX.Req.WXSceneTimeline;
            break;
        case WXSceneSession:
            req.scene=SendMessageToWX.Req.WXSceneSession;
            break;
    }
    api.sendReq(req);
}

这是我的统一代码:

public class test : MonoBehaviour {
   public GameObject btnObj;
   // Use this for initialization
   void Start () {
      Button button = btnObj.GetComponent<Button>();
      button.onClick.AddListener(share);
  }

  // Update is called once per frame
  void Update () {

  }

  void share()
  {
      AndroidJavaClass jc = new    AndroidJavaClass("com.unity3d.player.UnityPlayer");
      AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
      jo.Call("shareToWX", "unity test",0);
  }
 }

的Manifest.xml:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.yrgame.wjqpyx">

<application android:allowBackup="true" android:label="@string/app_name"
    android:supportsRtl="true">
    <activity android:name="com.yrgame.wjqpyx.wxapi.WXEntryActivity"/>
</application>
 </manifest>

错误:

 I/Unity: AndroidJavaException: java.lang.NoSuchMethodError: no non-static method with name='shareToWX' signature='(Ljava/lang/String;I)V' in class Lcom.unity3d.player.UnityPlayerActivity;
                                                          java.lang.NoSuchMethodError: no non-static method with name='shareToWX' signature='(Ljava/lang/String;I)V' in class Lcom.unity3d.player.UnityPlayerActivity;
                                                              at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source)
                                                              at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
                                                              at com.unity3d.player.UnityPlayer.c(Unknown Source)
                                                              at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
                                                              at android.os.Handler.dispatchMessage(Handler.java:98)
                                                              at android.os.Looper.loop(Looper.java:136)
                                                              at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
                                                            at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 
                                                            at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 
                                                            at UnityEngine.AndroidReflection.GetMetho

请帮我一把!!!

1 个答案:

答案 0 :(得分:1)

我相信这是因为你没有正确宣布你的清单所以它仍然使用正常的UnityPlayerActivity而不是你的自定义。

具体在清单中的活动标签内添加:

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

查看Unity在此处提供的示例:https://docs.unity3d.com/Manual/AndroidUnityPlayerActivity.html