Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

event problem #158

Open
htoooth opened this issue Jul 8, 2015 · 1 comment
Open

event problem #158

htoooth opened this issue Jul 8, 2015 · 1 comment

Comments

@htoooth
Copy link

htoooth commented Jul 8, 2015

I want to use otto in different activity.
I write some code:

public class MainActivity extends ActionBarActivity {

    @Bind(R.id.name)
    TextView name;

    public static final float DEFAULT_LAT = 40.440866f;
    public static final float DEFAULT_LON = -79.994085f;
    private static final float OFFSET = 0.1f;
    private static final Random RANDOM = new Random();


    public static float lastLatitude = DEFAULT_LAT;
    public static float lastLongitude = DEFAULT_LON;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button)
    public void SayHi(Button button){

        BusProvider.getInstance().post(new LocationClearEvent());

        lastLatitude = DEFAULT_LAT;
        lastLongitude = DEFAULT_LON;
        BusProvider.getInstance().post(produceLocationEvent());

        button.setText("hi");
    }

    @OnClick(R.id.change)
    public void ChangeLocation(){
        lastLatitude += (RANDOM.nextFloat() * OFFSET *2) -OFFSET;
        lastLongitude += (RANDOM.nextFloat() * OFFSET *2) -OFFSET;
        BusProvider.getInstance().post(produceLocationEvent());
    }

    @OnClick(R.id.view)
    public void changeView() {
        Intent intent = new Intent(this,MainActivity2Activity.class);
        startActivity(intent);
    }

    @Override protected void onResume() {
        super.onResume();

        // Register ourselves so that we can provide the initial value.
        BusProvider.getInstance().register(this);
    }

    @Override protected void onPause() {
        super.onPause();


        // Always unregister when an object no longer should be on the bus.
        BusProvider.getInstance().unregister(this);
    }


    @Produce
    static public LocationChangedEvent produceLocationEvent() {
        // Provide an initial value for location based on the last known position.
        return new LocationChangedEvent(lastLatitude, lastLongitude);
    }

    @Subscribe
    public void listenLocation(LocationChangedEvent event){
        Log.i("main", event.toString());
    }

Second Activity:

public class MainActivity2Activity extends ActionBarActivity {

    @Bind(R.id.location)
    TextView change;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        BusProvider.getInstance().register(this);
    }

    @Subscribe
    public void onLocationChanged2(LocationChangedEvent event){
        if(event == null) Log.i("null","event is null");
        Log.i("location change", event.toString());
        change.setText(event.toString());
    }


    @Override
    protected void onResume() {
        super.onResume();
        BusProvider.getInstance().register(this);
    }

    @Produce
    public LocationChangedEvent voichang(){
        LocationChangedEvent a = new LocationChangedEvent(1.1f,1.1f);
        return a;
    }

    @Override
    protected void onPause() {
        super.onPause();
        BusProvider.getInstance().unregister(this);
    }
}

When i start second activity. there is an error happened.

07-08 10:50:44.666  23212-23212/com.pp.kpm.test W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6270288)
07-08 10:50:44.666  23212-23212/com.pp.kpm.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pathprint.kpm.test/com.pathprint.kpm.test.MainActivity2Activity}: java.lang.RuntimeException: Could not dispatch event: class com.pathprint.kpm.test.LocationChangedEvent to handler [EventHandler public void com.pathprint.kpm.test.MainActivity2Activity.onLocationChanged2(com.pathprint.kpm.test.LocationChangedEvent)]: null
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

I don't know why?

@ManoyloK
Copy link

I seems like your TextView change = null, try BusProvider.getInstance().register(this) in onViewCreated

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants