otto使用报错

小弟在使用square/otto报错,具体如情况如下:

操作系统为window7,手机模拟器和真实的机器都试过,都不行。

具体情况如下:

我在MainActivity中是这样写的(跟官方的sample很像):

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());

}

第二个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);

}

}

LocationChangedEvent是这样的:

public class LocationChangedEvent {

public final float lat;

private final float lon;

public LocationChangedEvent(float lat,float lon){

this.lat = lat;

this.lon = lon;

}

@Override

public String toString(){

return new StringBuilder("(")

.append(lat)

.append(",")

.append(lon)

.append(")")

.toString();

}

}

运行是这样的图:

图片描述

单击第三个按钮绑定的是这个事件:

 @OnClick(R.id.view)

public void changeView() {

Intent intent = new Intent(this,MainActivity2Activity.class);

startActivity(intent);

}

跳过去,第二个Activity就崩溃了,出现了以下错误:

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)

网上搜了很多内容,都没有反映,不知道是哪里出问题了,请大家指导一下。

以上是 otto使用报错 的全部内容, 来源链接: utcz.com/p/167763.html

回到顶部