【问题标题】:Is there a way to implement setOnDateListener within a fragment and have it send data to another fragment?有没有办法在片段中实现 setOnDateListener 并将数据发送到另一个片段?
【发布时间】:2019-08-27 07:45:14
【问题描述】:

我在 android studio 工作,想为 CalendarView 对象设置 setOnDateListenerCalendarView 对象位于以编程方式生成的片段内。唯一的问题是尝试将数据从日历发送到MainActivity 文件时。它一直在说空对象引用。

我什么都试过了。目前我正在尝试使用我没有经验的界面。我试过到处移动听众。似乎没有任何效果。这是最小的(非常抱歉)

MainActivity:

public class MainActivity extends AppCompatActivity implements FragmentEntry.Callback {

    FragmentHome fragmentHome;

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

        ViewPager viewPager = findViewById(R.id.viewpager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        // add the fragments
        adapter.addFragment(new FragmentHome(), "Home");
        adapter.addFragment(new FragmentEntry(), "Entry");
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    public void sendDate(int day, int month, int year)
    {
        fragmentHome.text(day, month, year);
    }
}

FragmentHome:

    public class FragmentHome extends Fragment {

        // empty constructor, onCreate, and onCreateView

        public void text(int day, int month, int year) {
            TextView textView = getText().findViewById(R.id.textView);
            String text = String.format(R.string.month + "/" + R.string.day + "/" + R.string.year, month, day, year);
            textView.setText(text);
        }
    }

片段条目:

    public class FragmentEntry extends Fragment {

        public Callback callback;

        // empty constructor, onCreate

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.fragment_entry, container, false);

            CalendarView calendarView = v.findViewById(R.id.calendarView);
            calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(@NonNull CalendarView calendarView, int y, int m, int d) {
                    callback.sendDate(d, m, y);
        }

        return v;
    }

就像我说的那样,我尝试过在日期监听器周围移动,但它目前就在这里。我希望 sendDate 函数调用能够正常运行,但 Logcat 另有说明。一直在说

E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.fragmentcommunicationtest.FragmentEntry$Callback.sendDate(int, int, int)' on a null object reference

即使不使用接口,我对整个空对象引用也有同样的问题。我不知道还能去哪里。我已经尝试了一切,我希望在这一点上它会成为我忽略的东西。帮助

【问题讨论】:

  • 你需要初始化你的回调。请显示您从 MainActivity 创建 FragmentHome 的代码。
  • @sunilsunny 按要求添加了片段主页实现。
  • 好的,看看我的回答。
  • 我注意到的另一个错误是getText().findViewById(R.id.textView);。你找不到那样的风景。
  • @sunilsunny 我的错,意思是说getView()。我的错。

标签: java android android-fragments event-listener


【解决方案1】:

你没有初始化你的callback对象,从MainActivity试试这个

adapter.addFragment(new FragmentEntry(this), "Entry");

并在FragmentEntry 中创建一个以回调为参数的构造函数(我相信这就是您想要回调的地方)。

public FragmentEntry(Callback callback) 
{
 this.callback = callback
}

现在回调已初始化。

编辑

对于第二期TextView textView = getView().findViewById(R.id.textView);,这是错误的,因为您试图从TextView 中查找视图。

试试这个:

创建一个全局TextView,从onCreateView获取视图并设置在 回调。

TextView textView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_home, container, false);
    textView = v.findViewById(R.id.textView);
    return v;
}

然后像这样设置值。

public void text(int day, int month, int year) {
    String text = String.format(R.string.month + "/" + R.string.day + "/" + R.string.year, month, day, year);
    textView.setText(text);
}

编辑 2 你是对的 fragmentHome 确实是空的。试试这个

fragmentHome = new FragmentHome(); 
adapter.addFragment(fragmentHome, "Home");

【讨论】:

  • 好的,这让我比以往任何时候都走得更远,我真诚地感谢你。但现在它抱怨来自FragmentEntry 中的sendDate 函数对text(int, int, int) 的调用。很抱歉一直问这件事困扰了我一段时间。
  • 嘿,伙计。你是唯一真正帮助我解决这个问题的人,所以我还有另一个问题。我做了一些测试,发现问题来自fragmentHome.text()。即使有争论,它也会抱怨null object reference。我想也许我需要以不同的方式初始化fragmentHome
  • 老兄,我感激不尽。你是唯一一个愿意帮助我解决这个我已经坚持了将近 2 周的问题的人。你甚至不知道我有多感激。再次感谢!
  • 没关系,伙计。如果对您有帮助,您可以接受我的回答。
【解决方案2】:

推荐在 Fragment 之间或与 Activity 之间进行通信的方式是通过 ViewModel 组件。请参阅下文,了解 VieModel 相对于您的案例的接口通信方式的优势: https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多