从MainActivity传递带有bundle的字符串到android中的Fragment时遇到麻烦

时间:2018-04-12 17:48:30

标签: java android android-studio

将测试字符串从 MainActivity 传递到 TabFragment_malica1 时遇到问题。我得到"尝试在空对象上调用虚方法.." 错误。我可以看到bundle是一个带有 if语句的空对象,但我无法弄清楚我做错了什么。任何帮助将不胜感激!

主要活动

>>> from sympy import *
>>> from sympy.solvers.recurr import rsolve
>>> var('n')
n
>>> s = Function('s')
>>> f = s(n) - s(n-1) -s(n-2)
>>> rsolve(f, s(n), {s(0):1, s(1):1})
(1/2 + sqrt(5)/2)**n*(sqrt(5)/10 + 1/2) + (-sqrt(5)/2 + 1/2)**n*(-sqrt(5)/10 + 1/2)

FRAGMENT

public class MainActivity extends AppCompatActivity {

  String varTest = new String();
  String[] testArray = new String[3];

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(R.string.tab1));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);



    sendData();

    public void sendData() {

      varTest = "TEST-TEST-TEST";


      //PACKING DATA IN A BUNDLE
      Bundle bundle = new Bundle();
      bundle.putString("TEST", varTest);


      //PASSING DATA OVER THE BUNDLE TO FRAGMENT
      TabFragment_malica1 myFragment = new TabFragment_malica1();
      myFragment.setArguments(bundle);


      //TEST BUNDLE
      Log.d("MAIN FRAGMENT DATA:", String.valueOf(bundle));

    }
  }

0 个答案:

没有答案