0

当我启动应用程序并单击按钮转到活动时,我突然得到一个强制关闭。我试图把它全部抹掉,然后再写一遍,但它仍然存在。可能是什么问题呢?

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);

        }


    });

}
}

这是布局代码

<?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>
4

4 回答 4

3

您需要先setContentView致电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);
于 2012-07-19T16:27:23.437 回答
2

您需要调用setContentView(R.layout.yourxmlLayout);以在您的活动类上引用布局。

于 2012-07-19T16:28:02.157 回答
0

尝试从点击监听器中弹出一个 toast,看看它是否有效。如果是这样 - 那么问题出在您要启动的活动中。

于 2012-07-19T16:29:05.507 回答
0

您需要添加setContentView(R.layout.yourlayoutxmlname);.

试试下面的代码:

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);
            }
        });
    }
}
于 2012-07-19T16:56:09.893 回答