Unity的windowsFocusChanged()有时会给我NullPointerException

时间:2017-04-27 12:48:43

标签: android-studio android-fragments unity3d

我正在尝试将Unity项目导入Android Studio项目。我已经知道如何通过管理活动来实现这一目标,但现在我尝试使用片段进行操作,以便仅在屏幕的一部分上显示我的Unity场景,同时在另一部分上显示一些本机UI。 为此,我遵循了这个:link并修改了它以适应我的代码。

public class UnityManagerFragment extends MyFragmentManager {

    protected UnityPlayer mUnityPlayer;
    public FrameLayout unityFrameLayout;

    public UnityManagerFragment()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        mUnityPlayer = new UnityPlayer(getActivity());
        View view = inflater.inflate(R.layout.unity_manager_view, container, false);

        this.unityFrameLayout = (FrameLayout) view.findViewById(R.id.unity_fragment);
        this.unityFrameLayout.addView(mUnityPlayer.getView(), FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT);

        mUnityPlayer.requestFocus();
        mUnityPlayer.windowFocusChanged(true);
        return view;
    }
}

MyFragmentManager扩展Fragments并且没有做任何可能导致问题的事情,事实上,问题出现在我创建此类之前。 我的问题在于mUnityPlayer.windowFocusChanged(true);行,当我点击按钮启动我的Unity片段时,有时会崩溃我的应用程序。日志告诉我:

Caused by: java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.example.testfragments/com.example.testfragments.UnityFragmentContainerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6873).
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference
    at android.os.Message.sendToTarget(Message.java:416)
    at com.unity3d.player.UnityPlayer$b.a(Unknown Source)
    at com.unity3d.player.UnityPlayer$b.a(Unknown Source)
    at com.unity3d.player.UnityPlayer.windowFocusChanged(Unknown Source)
    at com.example.testfragments.UnityManagerFragment.onCreateView(UnityManagerFragment.java:43).
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
    at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1250)
    at android.app.Activity.performStart(Activity.java:6588)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3087)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6873)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

方法windowsFocusChanged来自unity-classes.jar,它是Unity在我导出项目时生成的,我无法对其进行修改。我必须使用它,否则我会得到黑屏而不是看到我的场景。 我不知道发生了什么,有人可以帮助我吗? 感谢。

2 个答案:

答案 0 :(得分:0)

str <- sample(sample(letters, replace = TRUE), size = 1.5e6, replace = TRUE) tab.str <- sort(unique(str)) # could use `letters` as lookup table str.int <- match(str, tab.str) all.equal(tab.str[str.int], str) 进行空指针检查有时片段管理器使用方法getActivity();返回活动的空引用。

答案 1 :(得分:0)

使用Unity2019。我正在使用2019.1.10f

,此问题已解决。