感谢您的帮助,如果这是一个非常愚蠢的问题,请原谅,我没有在其他地方找到答案。
我的目标是确定当ViewPager全部设置完毕时,它所拥有的页面总数,就像我的代码中一样。我尝试过在后面版本中将“size变量”添加到f作为它的参数(请见,FirstFragment)。它不是一个已知的数字,因为用户输入将要创建的表格(视图)数量,并将其作为意图传递给FirstFragment。所以我尝试过:
Fragment f = new UserFragment();
Bundle bundl = new Bundle();
bundl.putInt("pagenumbers", size);
f.setArguments(bundl);
lf.add(f);
在第一个Fragment中。但是当UserFragment提取其"pagenumbers"参数时,它返回0。 然后我试图访问FragmentAdapter内部的getCount()方法,但是,由于我在需要它的UserFragment中,我不知道如何处理。
我尝试做的事情可能有助于澄清,即我正在提取"当前页面信息",由于早期帖子和帮助我的人,我现在成功了。现在我想看看"currentpage == size(//即页数。Viewpager中的总视图数)"。因此,在UserFragment中可以看到的onClick方法中,如果是最后一页,并且用户点击按钮以转到下一页。我希望它跳转到另一个活动,该活动操作从所有表单收集的所有SparseArray数据。 (我需要帮助将这个SparseArray数据放入意图中,我可能会用一个新问题解决这个问题...因为它与相关性不太相关)。所以,如果您可以协助我使当前尝试正常工作(如何正确执行),或者只是建议全新的方法,我很高兴。只要我能让它工作!
非常感谢您提前的所有帮助。请帮帮我!
我的UserFragment.java:
package com.example.thinice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class UserFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_avgcalcform,
container, false);
final EditText markc = (EditText)rootView.findViewById(R.id.editTextASSMARK);
final EditText marktotc = (EditText)rootView.findViewById(R.id.editTextASSTOT);
final EditText assvalc = (EditText)rootView.findViewById(R.id.editTextASSWORTH);
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
String pagenum = Integer.toString(currentviewnum);
TextView tv = (TextView) rootView.findViewById(R.id.avg_testtv);
tv.setText(pagenum);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener( new View.OnClickListener(){
public void onClick(View v){
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
((FirstFragment) getActivity()).saveData(new Question(markc.getText().toString(),marktotc.getText().toString(),assvalc.getText().toString()));
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum+1);
}
});
return rootView;
}
}
如果您需要查看我的代码,请告诉我。但这真的很烦人。 所以,如果有人能帮助我,那就太棒了! 非常感谢您提前。
FragmentAdapter.java:
package com.example.thinice;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public FragmentAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
//Bundle args = new Bundle();
//args.putInt("page_position", position+1);
//((Fragment) fragments).setArguments(args);
return this.fragments.get(position);
}
@Override
public void destroyItem (ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
FirstFragment.java(这是我的活动):
package com.example.thinice;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.widget.Button;
public class FirstFragment extends FragmentActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_viewpager);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
int size = getIntent().getExtras().getInt("numass1");
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
if (savedInstanceState != null) {
return;
}
ArrayList<Fragment> lf = new ArrayList<Fragment>();
for(int count=0; count<size; count ++){
Fragment f = new UserFragment();
lf.add(f);
}
FragmentAdapter hello = new FragmentAdapter(getSupportFragmentManager() , lf);
vp.setAdapter(hello);
// vp.setOffscreenPageLimit(size);
}
SparseArray<Question> questions = new SparseArray<Question>();
public void saveData(Question quest){
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
questions.put(vp.getCurrentItem(), quest);
}
public ViewPager getPager() {
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
return vp;
}
}
编辑:建议解决方案的Logcat文件:
07-06 20:51:50.241: D/AndroidRuntime(8151): Shutting down VM
07-06 20:51:50.241: W/dalvikvm(8151): threadid=1: thread exiting with uncaught exception (group=0x415a4ba8)
07-06 20:51:50.251: E/AndroidRuntime(8151): FATAL EXCEPTION: main
07-06 20:51:50.251: E/AndroidRuntime(8151): Process: com.example.thinice, PID: 8151
07-06 20:51:50.251: E/AndroidRuntime(8151): java.lang.NullPointerException
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.example.thinice.UserFragment.onCreateView(UserFragment.java:34)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Handler.handleCallback(Handler.java:733)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Handler.dispatchMessage(Handler.java:95)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Looper.loop(Looper.java:136)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-06 20:51:50.251: E/AndroidRuntime(8151): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 20:51:50.251: E/AndroidRuntime(8151): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-06 20:51:50.251: E/AndroidRuntime(8151): at dalvik.system.NativeStart.main(Native Method)