小编Gar*_*ary的帖子

带有Fragments和EventBus的空指针异常

我已经通过各种方法使这个项目工作,并最终确定了Green Robot的EventBus.我在这里的第二个问题是: - 多个片段到活动的沟通.

我被带到那里的事件巴士路线.我在适当的图书馆里闯入,我认为我非常接近于它的工作.我不断在任何包含该行的行中获得空指针异常

eBus.post(Str);
Run Code Online (Sandbox Code Playgroud)

我相当肯定,因为我没有正确注册eBus.我知道Fragment没有这样的上下文,但我也尝试使用getActivity()也无济于事.我在下面发布了MainActivity,CoreFragment和LogCat.如果有人可以提供帮助,我会非常感激.这已经持续了近一个月!

import de.greenrobot.event.EventBus;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editText1;
String Txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText1 = (EditText) findViewById(R.id.editText1);
    Txt = "";
    EventBus eBus = new EventBus();
    eBus.register(this);<--This I removed.
        eBus.getDefault().register(this);<--This is the new line you suggested  
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it …
Run Code Online (Sandbox Code Playgroud)

android android-fragments greenrobot-eventbus

5
推荐指数
1
解决办法
6041
查看次数