我無法對使用片段中的函數的文本字段或任何對象進行更改。我該怎麼辦?Android - SetText中的片段不起作用
片段:
public class Benimsayfam extends Fragment{
TextView tv;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.benimsayfam,container,false);
tv = (TextView) v.findViewById(R.id.TtT);
return v;
}
public void update(){
tv.setText("Updated");
}
}
MainActivity:
public class MainActivity extends AppCompatActivity implements HttpRequest.AsyncResponse {
private ViewPager mViewPager;
SectionsPageAdapter mSectionPageAdapter;
BanaYazilanAdapter banaYazilanAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainactivity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tablayout = (TabLayout) findViewById(R.id.tabs);
tablayout.setupWithViewPager(mViewPager);
}
public void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Benimsayfam(),"Benim Sayfam");
adapter.addFragment(new Arges(),"Arges");
adapter.addFragment(new Bersd(),"Bersd");
viewPager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_s, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void processFinish(JSONObject object) throws JSONException {
Benimsayfam b = new Benimsayfam();
b.update();
}
}
我使用選項卡菜單。我需要在Tab轉換期間在MainActivity中使用一個函數。我在使用AsyncResponse時遇到問題,只要我可以在片段中完成。
tv =(TextView)v.findViewById(R.id.TtT);在oncreate – Raj
如何添加片段的活動只需調用onCreateView的update()方法 – Pavan
是否將「Benimsayfam」片段附加到Activity的佈局?我們可以看到更多關於MainActivity的代碼嗎? – rastadrian