【发布时间】:2014-08-19 19:43:31
【问题描述】:
我正在尝试更改 xml 页面中的 textview 以暗示 android 的滑动菜单。我尝试将代码放在主要活动的 oncreate 方法中,但它在启动时崩溃。我确实有另一个类,但它调用 oncreateview 而不是 oncreate,并且在 oncreateview() 中不存在 findviewbyid 方法。
我在创建这个应用程序时实现了 android 的滑动菜单。它有两个页面,一个输入页面和一个输出页面。我已经用文本视图、编辑文本和按钮填充了输入页面。我不确定在哪里更改文本视图。我的目标是将 textview 设置为今天的日期。
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
TextView tv = (TextView) findViewById(R.id.textView4);
// tv.setText("test post");
// tv.setText("test post");
}
public class inputClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.input_xml, container, false);
return rootView;
}
}
【问题讨论】: