@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.my_fragment, container, false);
textview=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if(bundle!=null){
System.out.println("not null");
textview.setText("The Page Currently Selected is"+bundle.getInt("position"));
}
System.out.println(" null");
return layout;
在上面的代码中,它表示不要使用SET TEXT显示文本显示,使用ANDROID RESOURCES INSTEAD,同样,我的viewpager不工作,它没有显示我的片段,我相信它与此有关,任何建议? 下面是我的主要活动的其余部分,我设置了我的viewpager,但它不能用我的片段或当我加载它????
public class MainActivity extends ActionBarActivity {
private Toolbar mToolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private static final String KEY_FRAGMENT="position";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
mToolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationFragment drawerlayout=(NavigationFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation);
drawerlayout.setUp(R.id.fragment_navigation,(DrawerLayout)findViewById(R.id.drawer_layout),mToolbar);
mPager=(ViewPager)findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs=(SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.navigate){
startActivity(new Intent(this, NavigateTo.class));
}
return super.onOptionsItemSelected(item);
}
class MyPagerAdapter extends FragmentPagerAdapter {
String[]tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment=MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 3;
}
//constructing a fragment class in the MainActivity(UPDATE THIS)
}
public static class MyFragment extends Fragment{
private TextView textview;
//in position represents the position that is shown inside the fragment
public static MyFragment getInstance(int position){
MyFragment myFragment=new MyFragment();
Bundle args=new Bundle();
//UPDATE PUT A KEY IN FOR "position"
args.putInt(KEY_FRAGMENT,position);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.my_fragment, container, false);
textview=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if(bundle!=null){
System.out.println("not null");
textview.setText("The Page Currently Selected is"+bundle.getInt("position"));
}
System.out.println(" null");
return layout;
}
}
}
答案 0 :(得分:1)
这里有两件事。
首先,您应该使用资源字符串和格式化方法。 在res / strings.xml中添加当前行
<string name="resource_id">"The Page Currently Selected is %d$1"</string>
因此,在您的代码中,您可以使用getString方法
getResources().getString(R.id._resource_id, bundle.getInt("position"));
此处提供更多信息:http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
对于第二点,它没有与之前的警告相关联,应该有更多我没有注意到的事情。也许你的textView的颜色与你背景的颜色相同,使它看不见。