我在我的应用程序中使用了一个标签视图...
我的代码......
public class ComboGraph extends AppCompatActivity {
Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combo_graph);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
Button next = (Button) findViewById(R.id.button8);
final TextView textView = (TextView) findViewById(R.id.textView22);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText().toString() + "a");
ExpensesGraph expensesGraph=(ExpensesGraph)getSupportFragmentManager().findFragmentById(R.id.fragment1);
expensesGraph.textView.setText("Hello");
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ExpensesGraph(), "Expenses");
adapter.addFragment(new IncomeGraph(), "Income");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
ExpenseGraph.java
public class ExpensesGraph extends Fragment {
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_epenses_graph, container, false);
textView = (TextView) view.findViewById(R.id.textview_fragment);
return view;
}
}
错误日志
java.lang.NullPointerException:尝试从字段中读取&#39; android.widget.TextView com.pocketdiary.ExpensesGraph.textView&#39;在null对象引用上 在com.gloesolutions.pocketdiary.ComboGraph $ 1.onClick(ComboGraph.java:44)
我需要从expensgraph
按钮点击combograph
活动值更改textview。我尝试的每个方法都得到零点错误。
我该如何解决这个问题。?
答案 0 :(得分:3)
为ExpensesGraph和IncomeGraph创建一个BaseFragment,如下所示
public abstract class BaseFragment extends Fragment {
public abstract void updateComponent();
}
在活动按钮Onclick
中Button next = (Button) findViewById(R.id.button8);
final TextView textView = (TextView) findViewById(R.id.textView22);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
(BaseFragment)viewPageAdaptor.getItem(0)).updateComponent();
}
});
在ExpensesGraph和IncomeGraph Fragments中,您需要相应地更新TextView。如果你想在所有片段中使用相同的回调使它在循环内。
片段内部
public class ExpensesGraph extends BaseFragment {
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_epenses_graph, container, false);
textView = (TextView) view.findViewById(R.id.textview_fragment);
return view;
}
public void updateComponent(){
if(textView != null){
textView.setText("Update your text Here")
}
}}
答案 1 :(得分:1)
您应该在mFragmentList.add(fragment);
调用替换方法,因为您的片段中的onCreateView
可能无法重新初始化
答案 2 :(得分:0)
你可以在活动中创建两个方法getter和setter,例如。 public String getDataToPass(){return requiredtext;},public void setDataToPass(String requiredtext){this.requiredtext = requiredtext;},onClick in fragment按钮,将数据传递给activity方法,例如。 btn.onClick(){getActivity()。setDataToPass(“your text”);},最后在下一个片段上OnViewCreated:获取文本为String text = getActivity()。getDataToPass();