更新: Logcat has been added to the question
我实际上想要在button Click
上更改视图(从一个片段到另一个片段。)我尝试了很多东西,但我无法做到。我还写了一个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)
答案 0 :(得分:23)
你在@ NPE
的第71行得到MyFragment
,正如你在评论中说的那样,这一行是:
mViewPager.setCurrentItem(1, true);
所以mViewPager
为空,您需要在使用
使用以下代码代替您的代码:
mViewPager.setCurrentItem(getItem(+1), true);
和getItem()
是:
private int getItem(int i) {
return mViewPager.getCurrentItem() + i;
}
答案 1 :(得分:4)
虽然不是答案,但请继续阅读
我可能会迟到,我从online resource找到了一个非常简单的解决方案:
在片段xml中,您要实现下一个/上一个按钮,将此行添加到按钮xml:
机器人:的onClick =&#34; jumpToPage&#34;
将光标放在&#34; jumpToPage&#34;之间。 - 按Alt + Enter:
在PagerActivity中添加相关功能 - 不在片段中
现在将功能修改为:
public void jumpToPage(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
}
现在给我买nunchai :)
答案 2 :(得分:1)
我希望这会对你有所帮助:
pager.setCurrentItem(1, true);
答案 3 :(得分:0)
我就是这样做的
if (viewPager.getCurrentItem() < 4) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
答案 4 :(得分:0)
只需执行此操作
下一个和上一个是两个按钮。
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewPager.getCurrentItem() != 0)
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount())
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
});
答案 5 :(得分:0)
我的代码是针对kotlin的,我认为我可以为很多人提供帮助,所以我不得不在这里编写它。
val fragments: ArrayList<Fragment> =
arrayListOf(
HomeFragment(),
SearchFragment(),
AddFragment(),
NotificationsFragment(),
ProfileFragment()
)
binding.viewpager.adapter = FragmentViewPagerAdapter(supportFragmentManager, fragments)
binding.viewpager.offscreenPageLimit = 5
binding.viewpager.currentItem = 1
binding.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
}
override fun onPageSelected(position: Int) {
when(position){
0 -> binding.navigation.selectedItemId = position
1 -> binding.navigation.selectedItemId = position
2 -> binding.navigation.selectedItemId = position
3 -> binding.navigation.selectedItemId = position
4 -> binding.navigation.selectedItemId = position
}
}
})
binding.navigation.setOnNavigationItemSelectedListener { it
it.let {
when(it.itemId){
R.id.nav_home -> {
binding.viewpager.currentItem = 0
return@setOnNavigationItemSelectedListener true
}
R.id.nav_search-> {
binding.viewpager.currentItem = 1
return@setOnNavigationItemSelectedListener true
}
R.id.nav_add -> {
binding.viewpager.currentItem = 2
return@setOnNavigationItemSelectedListener true
}
R.id.nav_notifications -> {
binding.viewpager.currentItem = 3
return@setOnNavigationItemSelectedListener true
}
R.id.nav_profile -> {
binding.viewpager.currentItem = 4
return@setOnNavigationItemSelectedListener true
}
else -> {
binding.viewpager.currentItem = 0
return@setOnNavigationItemSelectedListener true
}
}
}
答案 6 :(得分:0)
最好和最简单的答案是
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);