在我的活动A中,它有一个listView。单击listView时,它会将 ID 传递给Activity B,后者使用适配器显示与所选选项卡对应的数据。
活动A
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
Intent intent=new Intent(getApplicationContext(),ActivityB.class);
intent.putExtra("ID",ID);
startActivity(intent);
}
});
}
活动B
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
id=getIntent().getExtras().getString("ID");
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i=0; i<3; i++){
actionBar.addTab(actionBar.newTab().setText(item[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
TabsFragmentPagerAdapter
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
Fragment fragment=null;
if(index == 0) {
Bundle bundle = new Bundle();
bundle.putLong("ID", Edit.ID);
Log.e("ID", Edit.ID+"");
fragment.setArguments(bundle);
return new EditInformation();
}
if(index == 1)
return new Edit_WorkForce();
if(index == 2)
return new Edit_WorkDetails();
return null;
}
EditInformation
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_info = inflater.inflate(R.layout.edit_information, container, false);
Name=(EditText)edit_info.findViewById(R.id.Edit_name);
Status=(EditText)edit_info.findViewById(R.id.Edit_status);
Weather=(EditText)edit_info.findViewById(R.id.Edit_weather);
Date=(EditText)edit_info.findViewById(R.id.Edit_date);
ts = new com.example.project.myapplication.API.InfoAPI(getActivity());
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
ID=bundle.getLong("ID");
}
RetrieveInformation(ID);
return edit_info;
}
当我点击活动A中的列表时,应用程序崩溃并显示如下错误。
Process: com.example.project.myapplication, PID: 3103
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Fragment.setArguments(android.os.Bundle)' on a null object reference
at com.example.project.myapplication.Adapter.TabsFragmentPagerAdapter.getItem(TabsFragmentPagerAdapter.java:35)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
我可以从活动A获取ID,这是错误引用fragment.setArguments(bundle);
答案 0 :(得分:0)
在 TabsFragmentPagerAdapter 中,更改
Fragment fragment=null;
if(index == 0) {
Bundle bundle = new Bundle();
bundle.putLong("ID", Edit.ID);
Log.e("ID", Edit.ID+"");
fragment.setArguments(bundle);
return new EditInformation();
}
...
到
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
Fragment fragment=null;
if(index == 0) {
fragment = new EditInformation();
Bundle bundle = new Bundle();
bundle.putLong("ID", Edit.ID);
Log.e("ID", Edit.ID+"");
fragment.setArguments(bundle);
return fragment;
}
...
}
在设置参数之前,您需要初始化 EditInformation
片段。