【发布时间】:2014-11-12 14:02:34
【问题描述】:
我使用 Android Studio new -> 选项卡式活动向导来创建选项卡式活动。我通过另一个活动片段的意图打开该活动。我通过 Intent Extras 向这个选项卡式活动发送额外的数据。
我知道我应该在活动中捕获额外的意图,然后将这些数据放入片段参数中。我在我的应用程序中多次使用片段进行常规空白活动。但是我似乎迷失了在选项卡式活动代码中的确切位置,我应该额外捕获意图并声明片段的参数。任何人都可以提供任何方向吗?到目前为止,无论我将标准意图 extra-> 片段参数代码放在哪里,我都只会遇到错误。
这是我来自 Android Studio 模板的选项卡式活动代码:
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() 定位在“onCreate(Bundle savedInstanceState)”下,就像在其他空白活动中一样。但我也得到一个错误。
那么,什么是正确的方法来捕获选项卡式活动中的额外意图,然后将这些参数解析为在活动选项卡下显示内容的片段?
【问题讨论】:
-
尝试在 onCreate 方法中获取额外内容时会出现什么错误(应该这样做)? getIntent().getBooleanExtra("keyword", false);
-
哦,看在他妈的份上!非常抱歉耽误您的时间!问题是对于这个特定的活动,我的 putExtra() 中有错字。而且我认为问题出在我获取这些额外内容的方法中......自然,错误是 NullPointException (或类似的东西),原因是我正在寻找的额外内容没有设置。感谢您引导我朝着正确的方向前进!
标签: java android android-intent android-fragments tabs