【问题标题】:setOnClickListener makes Force ClosesetOnClickListener 强制关闭
【发布时间】:2012-07-19 16:25:59
【问题描述】:

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

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>

【问题讨论】:

    标签: android layout button forceclose


    【解决方案1】:

    你需要在findViewById之前调用setContentView

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          您需要添加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);
                      }
                  });
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-08-25
            • 2015-04-03
            • 2013-07-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多