android-can';t访问片段方法内部的视图。它返回空对象
我正在基于android studio的tabbedActivity预设构建一个应用程序,因此创建了片段,以便:android-can';t访问片段方法内部的视图。它返回空对象,android,android-fragments,methods,view,nullreferenceexception,Android,Android Fragments,Methods,View,Nullreferenceexception,我正在基于android studio的tabbedActivity预设构建一个应用程序,因此创建了片段,以便: private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
后来:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
DashboardTab tabDashboard = new DashboardTab();
return tabDashboard;
case 1:
VertretungsplanTab tabVertretungsplan = new VertretungsplanTab();
return tabVertretungsplan;
default:
return null;
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Dashboard";
case 1:
return "Vertretungsplan";
}
return null;
}
}
这一切都很好,但事实上,这是一种非常不常见的创建选项卡的方法,所以我的问题可能是由于对MainActivity.java的错误理解造成的
public class VertretungsplanTab extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.vertretungsplan_fragment, container, false);
return rootView;
}
public void setText(String textToBeSet, VertretungsplanTab tab){
TextView tv = (TextView) getView().findViewById(R.id.textView2);
}
现在问题就我所知是由视图引起的,因为它在我的方法中是一个空对象
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
我尝试了几种访问视图的方法,包括在这个片段中创建一个新变量,并在onCreateView()中将视图提交给它。
如果我在onCreatedView()中签入它,它还不是空对象
我还尝试在onCreateView()中创建TextView,但在onCreateView()之后,无法调用setText()
在互联网上搜索了几天后,如果一个更高级的开发人员能够得到一个解决方案,这将是一个很大的帮助 问题是
Fragment
事务是异步的,因此通常您不应该从活动中访问Fragment
。相反,您应该做的是在getItem
方法中创建片段时,通过.setArguments()
传递所有必要的数据。稍后在onViewCreated
中,您可以使用getArguments
调用访问之前传入的包
:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
DashboardTab tabDashboard = new DashboardTab();
Bundle bundle = new Bundle();
bundle.putString("USER_NAME", users.get(position));
tabDashboard.setArguments(bundle);
return tabDashboard;
case 1:
VertretungsplanTab tabVertretungsplan = new VertretungsplanTab();
return tabVertretungsplan;
default:
return null;
}
}
然后在片段中
:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.textView2).setText(getArguments().getString("USER_NAME"));
}
通过将数据传递到
DetailsFragment
来检查示例,这样会更容易:
public class VertretungsplanTab extends Fragment {
private View rootView;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.vertretungsplan_fragment, container, false);
tv = (TextView) rootView.findViewById(R.id.textView2);
return rootView;
}
public void setText(String textToBeSet, VertretungsplanTab tab){
tv.setText(textToBeSet);
}
好的,基本上你的问题不清楚你想在哪里调用
setText()
方法。如果您已经添加了要从中调用setText()
的位置,则会更好
但就我所能理解的情况而言,您必须在片段外部调用该方法,并且必须创建片段的对象。如果您从片段内部调用setText()
方法,比如在onViewCreated()
中,那么它将100%工作,但是如果您尝试这样调用该方法:
VertretungsplanTab tab = new VertretungsplanTab();
tab.setText("any text", tab);
它肯定会返回一个NullPointerException
,因为您已经创建了片段的一个新对象,并且我们知道一个对象只是类的蓝图,这意味着在对其进行任何修改后,它没有值
我经常使用碎片,也多次面对这些情况。如果您有另一个类想要更新片段中的文本,那么处理程序是您最好的选择
BroadcastReceivers
在这种情况下也是不错的选择,您也可以使用接口或抽象类来实现这一点。只要试着学习这些概念,因为当您想要将数据从一个类传递到另一个类时,这些是您拥有的最好的东西
使用接口的一个例子是。这只是一个例子,它与您的问题并不相似,但通过这一点,您将了解如何使用接口。就这么办吧……我希望这会有帮助
您还可以学习如何使用处理程序将数据从活动传递到片段或从片段传递到活动
如果我误解了你的问题,请详细解释,因为这是我从你的问题中能理解的最多的:)好吧,这正是我想做的:)非常感谢,这是一个很大的帮助!