来自活动的Android片段文本视图setText
我有一个问题:我有两个标签的TabLayout。一个选项卡包含文本视图。 我想稍后从MainActivity中的线程更改MainActivity中TextView的文本。但它抛出了一个NPE,我不知道如何解决它 以下是我的MainActivity.java-code:来自活动的Android片段文本视图setText,android,android-studio,android-fragments,Android,Android Studio,Android Fragments,我有一个问题:我有两个标签的TabLayout。一个选项卡包含文本视图。 我想稍后从MainActivity中的线程更改MainActivity中TextView的文本。但它抛出了一个NPE,我不知道如何解决它 以下是我的MainActivity.java-code: public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private
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) {
// ...
}
}
}
下面是带有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);
}
}您可以在main活动的片段中调用setText方法,如下所示:
FragmentManager fm = getSupportFragmentManager();
//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.setText();
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
如果您通过代码添加了片段,并且在添加片段时使用了标记字符串,请改用findFragmentByTag,如下所示:
FragmentManager fm = getSupportFragmentManager();
//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.setText();
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
希望有帮助。在onCreate的末尾,您正在创建新的片段并尝试将其设置为text。因为它没有膨胀,所以你得到NPO
你们想要的是抓住已经膨胀的碎片并在上面设置文本。
请尝试getSupportFragmentManager.findFragmentByIdR.id.your_fragment.setText是否可以添加异常详细信息?getView似乎返回null。在MainActivity的onResume中调用此方法,在案例1中:return选项卡;而不是创建新的TabTerminal