gpt4 book ai didi

java - 从 Activity 更改 viewpager 中的 textView 值

转载 作者:行者123 更新时间:2023-12-01 17:55:54 24 4
gpt4 key购买 nike

我在我的应用程序中使用选项卡 View ...

我的代码...

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: Attempt to read from field 'android.widget.TextView com.pocketdiary.ExpensesGraph.textView' on a null object reference at com.gloesolutions.pocketdiary.ComboGraph$1.onClick(ComboGraph.java:44)

我需要通过单击combograph按钮来更改expensgraph Activity 值中的 TextView 。我尝试的每种方法都会出现空点错误。我该如何解决这个问题?

最佳答案

为 ExpensesGraph 和 IncomeGraph 创建一个 BaseFragment,如下所示

  public abstract class BaseFragment extends Fragment {
public abstract void updateComponent();
}

在 Activity 按钮中单击

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 fragment 内,您需要相应地更新 TextView。如果您希望在所有 fragment 中使用相同的回调,请使其在循环内。

fragment 内部

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")
}
}}

关于java - 从 Activity 更改 viewpager 中的 textView 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44794671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com