AlexGasconB
AlexGasconB

Reputation: 175

setOnClickListener makes Force Close

When I launch the application and click the button to go to the Activity, I suddenly get a Force Close. I've tried to erase it all and write it again, but it is still there. What could be the problem?

package com.alexgascon.formuladora; 

import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;



public class Matematicas extends Activity {

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
    final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
    final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);


    BtnMCD.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
            startActivity(MCDintent);

        }


    });

}
}

And here is the layout code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/pizarramatesverde"
 android:gravity="center" >

<Button
    android:id="@+id/ecsegundogrado"
    android:layout_height="wrap_content"
    android:layout_width="265sp"
    android:text="@string/ecsegundo"
    android:textColor="@color/Negro"
    />
<Button
    android:id="@+id/fracciones"
    android:layout_height="wrap_content"
    android:layout_width="265sp"
    android:text="@string/fracciones"
    android:layout_below="@id/ecsegundogrado"
    android:textColor="@color/Negro"
    />

<Button
    android:id="@+id/maximocomunBoton"
    android:layout_height="wrap_content"
    android:layout_width="265sp"
    android:text="@string/maximocomun"
    android:layout_below="@id/fracciones"
    android:layout_marginBottom="80sp"
    android:textColor="@color/Negro"
    />



</RelativeLayout>

Upvotes: 0

Views: 677

Answers (4)

AITAALI_ABDERRAHMANE
AITAALI_ABDERRAHMANE

Reputation: 2519

You need to add the setContentView(R.layout.yourlayoutxmlname);.

Try the following code:

package com.alexgascon.formuladora; 

import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Matematicas extends Activity {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayoutxmlname);

        final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
        final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
        final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);

        BtnMCD.setOnClickListener( new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
                startActivity(MCDintent);
            }
        });
    }
}

Upvotes: 0

Beauty Contest
Beauty Contest

Reputation: 19

Try to pop a toast from the click listener and see if it works. If it does - then the problem is in the activity that you want to launch.

Upvotes: 0

Nuno Gon&#231;alves
Nuno Gon&#231;alves

Reputation: 6795

You need to call setContentView(R.layout.yourxmlLayout); to have the layout being referenced on your activity class.

Upvotes: 2

MByD
MByD

Reputation: 137282

You need to call setContentView before findViewById:

setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);

Upvotes: 3

Related Questions