1

Tengo el siguiente Fragment que al apretar el botón me dirijo a MainActivity_program :

PageFragment3:

 public class PageFragment3 extends Fragment {

    Button btn;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.page_3, container,false);

        btn = rootView.findViewById(R.id.boton_empezar);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });

        return rootView;

    }

    public void start(){
        Intent intent = new Intent(getActivity(),MainActivity_program.class);
        startActivity(intent);
    }


}

y MainActivity_program:

public class MainActivity_program extends AppCompatActivity {

    GridLayout mainGrid;
    private Animation animacion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);


        mainGrid = (GridLayout) findViewById(R.id.mainGrid);
        animacion = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animacion_pulsar);

        //Set Event
        setSingleEvent(mainGrid);
        //setToggleEvent(mainGrid);
    }

    private void setToggleEvent(GridLayout mainGrid) {
        //Loop all child item of Main Grid
        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            //You can see , all child item is CardView , so we just cast object to CardView
            final CardView cardView = (CardView) mainGrid.getChildAt(i);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (cardView.getCardBackgroundColor().getDefaultColor() == -1) {
                        //Change background color
                        cardView.setCardBackgroundColor(Color.parseColor("#FF6F00"));
                        Toast.makeText(MainActivity_program.this, "State : True", Toast.LENGTH_SHORT).show();

                    } else {
                        //Change background color
                        cardView.setCardBackgroundColor(Color.parseColor("#FFFFFF"));
                        Toast.makeText(MainActivity_program.this, "State : False", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void setSingleEvent(GridLayout mainGrid) {
        //Loop all child item of Main Grid
        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            //You can see , all child item is CardView , so we just cast object to CardView
            final CardView cardView = (CardView) mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    cardView.startAnimation(animacion); // PARA LA ANIMACION CUANDO PULSES CUALQUIER CARDVIEW
                    Intent intent = new Intent(MainActivity_program.this,ActivityOne.class);
                    intent.putExtra("info","This is activity from card item index  "+finalI);
                    startActivity(intent);

                }
            });
        }
    }
}

Dándome el siguiente error:

2019-07-24 18:22:33.955 11278-11285/? E/art: Failed sending reply to debugger: Broken pipe 2019-07-24 18:22:40.014 11278-11278/com.example.imagenes E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.imagenes, PID: 11278 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagenes/com.example.imagenes.MainActivity_program}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.GridLayout.getChildCount()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.GridLayout.getChildCount()' on a null object reference at com.example.imagenes.MainActivity_program.setSingleEvent(MainActivity_program.java:72) at com.example.imagenes.MainActivity_program.onCreate(MainActivity_program.java:43) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 respuesta 1

0

El problema se indica aquí:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagenes/com.example.imagenes.MainActivity_program}: java.lang.NullPointerException: Attempt to invoke virtual method 'int

El Intent para cargar la Activity funciona correctamente pero al cargar MainActivity el valor de mainGrid es null al llamar el método setSingleEvent(), por esta razón no se carga la Activity.

private void setSingleEvent(GridLayout mainGrid) {
    //Loop all child item of Main Grid
    for (int i = 0; i < mainGrid.getChildCount(); i++) {
        ...
        ...

El problema se debe seguramente a que el GridLayout con id mainGrid no existe en el archivo splash_screen.xml que carga tu clase MainActivity_program .

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);


    mainGrid = (GridLayout) findViewById(R.id.mainGrid);
    ...
    ...
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.