尝试使用createNewLayerBuilder()

时间:2017-08-22 17:55:25

标签: android cyborg

这就是我在Application类中所做的:

@Override
public void onCreate() {
    super.onCreate();
    CyborgBuilder.startCyborg(new CyborgConfiguration(this, R.layout.cyborgview__auto_reply, BasicModulePack.class));
}

这就是cyborgview__auto_reply.xml的样子:

<?xml version="1.0" encoding="utf-8"?>
<com.nu.art.cyborg.core.CyborgView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cyborg="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
cyborg:controller="com.hedshafran.autoreply.controllers.Controller_PermissionScreen"
cyborg:tag="Controller_Permission"/>

这就是我在Controller_PermissionScreen类中导致崩溃的原因:

createNewLayerBuilder().setControllerType(Controller_MainScreen.class).setLayoutId(R.layout.controller__main_screen).build();

导致崩溃,如下所示:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hedshafran.autoreply, PID: 17910
java.lang.RuntimeException: Unable to resume activity {com.hedshafran.autoreply/com.nu.art.cyborg.core.CyborgActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.nu.art.cyborg.core.CyborgStackController$StackLayerBuilder com.nu.art.cyborg.core.CyborgStackController.createLayerBuilder()' on a null object reference
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3791)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3832)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6682)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.nu.art.cyborg.core.CyborgStackController$StackLayerBuilder com.nu.art.cyborg.core.CyborgStackController.createLayerBuilder()' on a null object reference
  at com.nu.art.cyborg.core.CyborgController.createNewLayerBuilder(CyborgController.java:506)
  at com.hedshafran.autoreply.controllers.Controller_PermissionScreen.onResume(Controller_PermissionScreen.java:35)
  at com.nu.art.cyborg.core.CyborgController.dispatchLifeCycleEvent(CyborgController.java:270)
  at com.nu.art.cyborg.core.CyborgActivityBridgeImpl.dispatchLifecycleEvent(CyborgActivityBridgeImpl.java:476)
  at com.nu.art.cyborg.core.CyborgActivityBridgeImpl.onResume(CyborgActivityBridgeImpl.java:213)
  at com.nu.art.cyborg.core.CyborgActivity.onResume(CyborgActivity.java:155)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1277)
  at android.app.Activity.performResume(Activity.java:7088)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3768)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3832) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:154) 
  at android.app.ActivityThread.main(ActivityThread.java:6682) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

如何避免崩溃并以正确的方式打开新的CyborgController?

1 个答案:

答案 0 :(得分:1)

您的 cyborgview__auto_reply.xml 应该使用包含Controller_PermissionScreen的CyborgStackConroller,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<com.nu.art.cyborg.core.CyborgView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cyborg="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/CV_RootStack"
    cyborg:controller="com.nu.art.cyborg.core.CyborgStackController"
    cyborg:rootController="com.hedshafran.autoreply.controllers.Controller_PermissionScreen"

    cyborg:rootTag="Controller_Permission"
    cyborg:tag="RootStack"
    />

要点是,如果调用此方法的控制器在堆栈中,则只能使用 createNewLayerBuilder api。否则使用 getControllerById(R.id.CV_RootStack)并创建一个新的图层构建器。