【问题标题】:Calling a Fragment Method in an Activity causes a Null Pointer Exception在 Activity 中调用 Fragment 方法会导致空指针异常
【发布时间】:2017-07-24 19:27:13
【问题描述】:

我想在单击 FloatingActionButton 时调用 Fragment 方法。
我正在使用黄油刀。

错误:

PID:9956 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.fischerdaniel.testapp.FirstFragment.setTvText()”

MainActicity 部分:

  @OnClick({R.id.fab_one, R.id.fab_two, R.id.fab_three}) public void test(View view) {
        switch (view.getId()) {
            case R.id.fab_three:
                FirstFragment firstFragment = (FirstFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_first);
                firstFragment.setTvText();
                break;
            default:
                break;
        }
    }

片段部分:

    @BindView(R.id.et_one) EditText etOne;
    @BindView(R.id.et_two) EditText etTwo;
    @BindView(R.id.tv_one) TextView tvOne;
    @BindView(R.id.tv_two) TextView tvTwo;

    private Unbinder unbinder;

   @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    protected void setTvText() {
        tvOne.setText(etOne.getText());
        tvTwo.setText(etTwo.getText());
    }

【问题讨论】:

  • 你是通过引用来称呼它

标签: android xml android-activity fragment butterknife


【解决方案1】:

查看此链接:findFragmentById always returns null

创建片段时,请使用:

add(int id, Fragment fragment, String tag) 

然后要获取您的片段,请使用:

findFragmentByTag(String tag)

【讨论】:

  • 片段在浏览器中
【解决方案2】:
public void onMenuItemClick(View clickedView, int position) {
    switch (position){
        case 1:
             fragmentChanger("Notifications",new notifyFragment());
           break;
        case 2:
             fragmentChanger("QR Code",new qrFragment());
            break;
        case 3:
            fragmentChanger("User ID",new MainFragment());
            break;
        case 4:
            fragmentChanger("Settings",new settingsFragment());
            break;
        default:
            Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
            break;
    }
}

//function that does the trasaction when a fragment object is passed
//R.id.container is my default frame
public void fragmentChanger(String title, Object expectedFragment){
   // mToolBarTextView.setText(title);
    transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.container, (Fragment) expectedFragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
}

【讨论】:

【解决方案3】:

从 Activity 调用您的片段方法,如下所示,

对我有用

FirstFragment firstFragment =(FirstFragment)getSupportFragmentManager().getFragments().get(0); 

firstFragment.yourmethod();

这里 0 是您的片段位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    相关资源
    最近更新 更多