简体   繁体   中英

Android Fragment TextView setText from activity

I'm having a problem: I have TabLayout with two tabs. One Tab contains a TextView. I want to change the text of the TextView from MainActivity (later from a Thread in MainActivity). But it throws an NPE and I don't know how to solve it.

Here is my MainActivity.java-code:

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

public static ThreadConnected myThreadConnected;
TabTerminal tabT;

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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

    tabT = new TabTerminal();
    tabT.setText("324");
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        //Returning the current tabs
        switch (position) {
            case 0:
                TabControl tab1 = new TabControl();
                return tab1;
            case 1:
                TabTerminal tab2 = new TabTerminal();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // ...
    }
}

}

And here is the Fragment with TextView:

public class TabTerminal extends Fragment {

static TextView txtterminalout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_terminal, container, false);
    TextView txtterminalout = (TextView) rootView.findViewById(R.id.terminalout);

    return rootView;
}

public void setText(String text) {
    TextView t = (TextView) getView().findViewById(R.id.terminalout);
    t.setText(text);
}

}

You can call your setText() method in your fragment from your MainActivity like this:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment =  (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.setText();

If you added fragment via code and used a tag string when you added your fragment , use findFragmentByTag instead like this:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");

Hope it helps.

At the end of onCreate() you are creating new Fragment and trying to setText() to it. Since it is not inflated you are getting NPO.

What you want is to grab already inflated fragment and setText() on it. Try getSupportFragmentManager().findFragmentById(R.id.your_fragment).setText()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM