更新:Logcat已添加到问题中
实际上我想在按钮单击
时更改视图(从一个片段到另一个片段)。我尝试了很多方法,但无法做到。我还编写了一个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);
}
我的自定义ViewPager的代码
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;
}
}
日志记录器:
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)
MyFragment
。 - Shayan Pourvatan