我正在开发一个应用程序,我从服务器获取数据并在 MainActivity 中的 AsyncTask 中解析它 然后我调用另一个名为 DetailsPage 的活动,其中包含片段。 这是我如何调用并将参数传递给DetailsPage。这在AsyncTask中调用。
Intent myIntent = new Intent(MainActivity.this, DetailsPage.class);
myIntent.putExtra("adminId",adminId);
startActivity(myIntent);
}
然后在DetailPage中我得到这些参数并将它放在包中,以便由DetailsPage运行片段。
public class DetailsPage extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Payments", "Goods" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_page);
/*Get Extra Values from the mainActivity*/
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("adminId");
Bundle bundle=new Bundle();
bundle.putString("adminId", value);
PaymentsFragment payment_frag=new PaymentsFragment();
payment_frag.setArguments(bundle);
}
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
在PaymentsFragment中我试图从 oncreateView
中的字符串中获取字符串public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*Get Extras Passed by MainActivity*/
adminId=getArguments().getString(adminId);
View rootView = inflater.inflate(R.layout.fragment_payments, container, false);
CustomListView = this;
String serverURL = "http://ott.edgeaccounts.com/Webservices/GetPaymentService.php";
new GetPayments().execute(serverURL);
Resources res =getResources();
list=(ListView)rootView.findViewById(R.id.list_payments);
adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res);
list.setAdapter(adapter);
return rootView;
}
但它在此行中给出了空指针异常 的 adminId = getArguments()的getString(adminId); 提前感谢您的帮助。
答案 0 :(得分:2)
更改此
adminId=getArguments().getString(adminId);
到
adminId=getArguments().getString("adminId");
因为密钥是" adminId"
bundle.putString("adminId", value); // in quotes adminId
您可以参考