我使用Android Studio新版 - >选项卡式活动向导,用于创建选项卡式活动。我通过另一个活动片段的意图打开了这个活动。我通过附加意图将附加数据发送到此选项卡式活动。
我理解我应该在活动中捕获intent extras然后将这些数据放入片段参数中。我已经在我的应用程序中多次使用片段进行常规空白活动。但我似乎迷失了标签活动代码,我应该抓住额外的意图并声明片段的参数。任何人都可以提供任何指示吗?截至目前,我只有错误,无论我把标准意图放在哪里 - >片段参数代码。
这是Android工作室模板的标签式活动代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import java.util.Locale;
public class therapy_screen extends ActionBarActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_therapy_screen);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
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));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return therapy_screen_fragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
}
我试图抓住并解析额外的意图:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return therapy_screen_fragment.newInstance(position + 1, getIntent().getExtras().getString("btnId"));
}
但是我收到了一个错误。我还尝试将getExtras()置于&#34; onCreate(Bundle savedInstanceState)&#34;就像我在其他空白活动中一样。但我也得到一个错误。
那么,在选项卡式活动中捕获意图附加功能的正确方法是什么,然后将这些参数解析为在活动选项卡下显示内容的片段?
答案 0 :(得分:0)
哦,为了f ** k的缘故!我很抱歉花时间!问题是,对于这个特定的活动,我在putExtra()中输入了错字。而且我认为问题在于我获取这些额外内容的方法......当然,错误是NullPointException(或类似的东西),原因是我想要的额外设置没有设置。感谢NSimon指导我正确的方向发表评论!
因此,在onCreate方法中获取额外内容是正确的。只是为了澄清是否有人陷入同样的困境。
最后我使用的解决方案已经在答案中发布了(但由于putExtra中的拼写错误而导致错误):
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return therapy_screen_fragment.newInstance(position + 1, getIntent().getExtras().getString("btnId"));
}