0
I have a button that advances a viewpager, but I did it inside a include, because I use on several screen, but when I try to click on it it me error Nullpointerexception, I have checked the ids, if it has repeated but apparently ta everything ok.
My XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dia1s2layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
android:fontFamily="@font/scada"
android:text="Pesquise qualquer palavra que não compreenda."
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView6"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="Evite o máximo o uso da tradução"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
<include
layout="@layout/btnprox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
public class DiaFragment extends Fragment {
private static final String TAG = "igr";
private CustomViewPager viewPager;
private PlayPauseView btn_audio;
private FancyButton btn_prox;
int posicaolayoutaudio;
private int[] mlayouts;
private int[] mlayoutslayout;
int layoutaudioposicao;
private ViewGroup containerdia;
private boolean finalizar;
public DiaFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mlayouts = getArguments().getIntArray("layouts");
mlayoutslayout = getArguments().getIntArray("layoutslayout");
layoutaudioposicao = getArguments().getInt("layoutaudioposicao");
//View rootview = inflater.inflate(mlayouts[posicao], container, false);
//containerdia = rootview.findViewById(R.id.containerdia);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.diafragment, container, false);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = view.findViewById(R.id.viewpagerdias);
viewPager.setPagingEnabled(false);
viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
int resId;
View view;
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resId = mlayouts[position];
view = inflater.inflate(resId, null);
//botão proximo
View myLayout = view.findViewById( mlayoutslayout[position] );
btn_prox = myLayout.findViewById(R.id.btn_prox);
btn_prox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mudarpagina();
}
});
//botao audio
View includeaudio = view.findViewById(R.id.includeaudio);
if (includeaudio != null){
btn_audio = myLayout.findViewById(R.id.btn_audio);
btn_audio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "audio", Toast.LENGTH_SHORT).show();
}
});
}
container.addView(view);
return view;
}
@Override
public int getCount() {
return mlayouts.length;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == mlayouts.length-1) {
btn_prox.setText(getString(R.string.terminar));
btn_prox.setIconResource("");
btn_prox.setBackgroundColor(Color.parseColor("#3b5998"));
finalizar = true;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void mudarpagina() {
if (finalizar){
Log.d(TAG, "onClick: Entrou no finalizar");
Fragment fragment = new PreparacaoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentmain, fragment);
ft.commit();
}else if (viewPager.getCurrentItem() != mlayouts.length){
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
}
}
Here I pass the layout as arguments pro Fragment:
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btndia1:
FragmentTransaction ft = getFragmentManager().beginTransaction();
DiaFragment fragmentdiapreparacao = new DiaFragment();
Bundle args = new Bundle();
//SETAR LAYOUTS
int[] layouts = {R.layout.dia1s1, R.layout.dia1s2, R.layout.dias1s3};
args.putIntArray("layouts",layouts);
int[] layoutslayout = {R.id.dia1s1layout, R.id.dia1s2layout, R.id.dia1s3layout};
args.putIntArray("layoutslayout",layoutslayout);
//PASSAR LAYOUT QUE CONTEM AUDIO [ POSICAO ]
args.putInt("layoutaudioposicao",3);
fragmentdiapreparacao.setArguments(args);
ft.replace(R.id.contentmain, fragmentdiapreparacao);
ft.commit();
break;
}
}
Stacktrace:
12-18 14:25:18.511 23084-23084/doupenglish.com.br.doup E/AndroidRuntime: FATAL EXCEPTION: main
Process: doupenglish.com.br.doup, PID: 23084
java.lang.NullPointerException: Attempt to invoke virtual method 'void mehdi.sakout.fancybuttons.FancyButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at doupenglish.com.br.doup.Fragments.DiaFragment$1.instantiateItem(DiaFragment.java:72)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:605)
at doupenglish.com.br.doup.Fragments.DiaFragment.mudarpagina(DiaFragment.java:165)
at doupenglish.com.br.doup.Fragments.DiaFragment.access$300(DiaFragment.java:26)
at doupenglish.com.br.doup.Fragments.DiaFragment$1$1.onClick(DiaFragment.java:75)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:6244)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
I realized that only occurs the error, when I pass a third layout for arguments to Fragment, before I was passing only the first two.
case R.id.btndia1:
FragmentTransaction ft = getFragmentManager().beginTransaction();
DiaFragment fragmentdiapreparacao = new DiaFragment();
Bundle args = new Bundle();
//SETAR LAYOUTS
int[] layouts = {R.layout.dia1s1, R.layout.dia1s2,R.layout.dias1s3};
args.putIntArray("layouts",layouts);
int[] layoutslayout = {R.id.dia1s1layout, R.id.dia1s2layout,R.id.dia1s3layout};
args.putIntArray("layoutslayout",layoutslayout);
//PASSAR LAYOUT QUE CONTEM AUDIO [ POSICAO ]
//args.putInt("layoutaudioposicao",3);
fragmentdiapreparacao.setArguments(args);
ft.replace(R.id.contentmain, fragmentdiapreparacao);
ft.commit();
break;
}
If you need more code, just ask
Have to post the exception stacktrace?
– Jefferson Quesado
@Jeffersonquesado ready to go
– Igor Oliveira
I’ve edited and edited something that I noticed below
– Igor Oliveira