更新: Logcat has been added to the question
我实际上想将button Click
的 View (从一个 fragment 更改为另一个 fragment ),我已经尝试了很多事情,但是我做不到。我还写了一个CustomViewPager
来停止在滑动屏幕时更改页面。它工作正常。
这是我的代码:
MyFragment
public CustomViewPager mViewPager;
public Button next;
public static int count=0;
public static final MyFragment newInstance(String message,ArrayList<String> a)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
bdl.putStringArrayList(EXTRA_ARRAY, a);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
spinner = (Spinner)v.findViewById(R.id.spinoptions);
answer = (EditText)v.findViewById(R.id.answer);
next = (Button)v.findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(1, true);//getItem(-1) for previous
// it is not working
}
});
ArrayList<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
Builtprofile.q.get(0).getOptions();
answer.setVisibility(View.INVISIBLE);
spinner.setVisibility(View.VISIBLE);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Builtprofile.context,
android.R.layout.simple_spinner_item,getArguments().getStringArrayList(EXTRA_ARRAY));
dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(dataAdapter);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
return v;
}
private int getItem() {
return mViewPager.getCurrentItem();
}
我的PageAdapter的代码
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
ActivityClass的代码
public List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
for(i=0;i<q.size();i++)
{
fList.add(MyFragment.newInstance(q.get(i).getText(),q.get(i).getOptions()));
}
return fList;
}
public void fragmentopener()
{
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
myCustomViewPager的代码
public class CustomViewPager extends ViewPager {
private boolean isPagingEnabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.isPagingEnabled = false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
LogCat:
04-07 15:55:53.254: E/AndroidRuntime(17379): FATAL EXCEPTION: main
04-07 15:55:53.254: E/AndroidRuntime(17379): java.lang.NullPointerException
04-07 15:55:53.254: E/AndroidRuntime(17379): at adapters.MyFragment$1.onClick(MyFragment.java:71)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.view.View.performClick(View.java:3574)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.view.View$PerformClick.run(View.java:14293)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.os.Handler.handleCallback(Handler.java:605)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.os.Looper.loop(Looper.java:137)
04-07 15:55:53.254: E/AndroidRuntime(17379): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-07 15:55:53.254: E/AndroidRuntime(17379): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:55:53.254: E/AndroidRuntime(17379): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 15:55:53.254: E/AndroidRuntime(17379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-07 15:55:53.254: E/AndroidRuntime(17379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-07 15:55:53.254: E/AndroidRuntime(17379): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您在第71行@ NPE
上获得了MyFragment
,正如您在评论中所说的,此行是:
mViewPager.setCurrentItem(1, true);
因此
mViewPager
为null,您需要在使用之前对其进行初始化使用以下代码代替您的代码:
mViewPager.setCurrentItem(getItem(+1), true);
getItem()
是:private int getItem(int i) {
return mViewPager.getCurrentItem() + i;
}