我正在关注此SO answer。但我在这一行上有错误cannot resolve method setarguments
details.setArguments(getIntent().getExtras());
这是代码
public class SingleViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
MainActivity details = new MainActivity();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
/*
pageAdapter = new MyPageAdapter (getSupportFragmentManager(), fragments);
ViewPager pager=(ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);*/
// pageAdapter = new MyPageAdapter (getSupportFragmentManager(), fragments);
ViewPager pager=(ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<Fragment> getFragments() {
List<Fragment> flist = new ArrayList<Fragment>();
flist = new ArrayList<Fragment>();
flist.add(MyFragment.newInstance("frag1"));
flist.add(MyFragment.newInstance("frag2"));
flist.add(MyFragment.newInstance("frag3"));
return flist;
}
public static class MyPageAdapter extends PagerAdapter {
//private List<Fragment> fragments;
Context mContext;
LayoutInflater mLayoutInflater;
public MyPageAdapter(Context context)
{
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view ==((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
View itemView =mLayoutInflater.inflate(R.layout.pager_item,container,false);
ImageView imageView= (ImageView) itemView.findViewById(R.id.viewpager);
String Item = getActivity().getIntent().getExtras().getString("name");
Bundle bundle = this.getArguments();
//Bundle bundle = getIntent().getExtras();
ArrayList<Listitem> personArrayList = bundle.getParcelableArrayList("Person_List");
if (personArrayList != null && !personArrayList.isEmpty()) {
for (Listitem person : personArrayList) {
Picasso.
with(this).
load(person.url)
.placeholder(R.drawable.logo)
.fit()
.noFade()
.into(imageView);
Log.i("PersonsActivity",String.valueOf(person.url));
}
}
imageView.setImageResource();
container.addView(itemView);
return itemView;
}
@Override
public int getCount()
{
return this.fragments.size();
}
}
}
答案 0 :(得分:1)
在您提供的链接中,setArguments()
在片段上调用,而非活动。
在您的代码中,您尝试将活动添加为片段:
MainActivity details = new MainActivity();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
定义要添加到SingleViewActivity
的片段并改为使用它。