Java 只有MainActivity按钮起作用
应用程序的MainActivity有5个按钮,所有按钮都工作正常,但当我调用第三个屏幕时,应用程序停止。我不知道为什么会这样Java 只有MainActivity按钮起作用,java,android,android-button,Java,Android,Android Button,应用程序的MainActivity有5个按钮,所有按钮都工作正常,但当我调用第三个屏幕时,应用程序停止。我不知道为什么会这样 package com.example.victorsales.testeapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public clas
package com.example.victorsales.testeapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class teladoimc extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teladoimc);
}
public void telaadulto(View v){
Intent tela = new Intent(this, imcadulto.class);
startActivity(tela);
}
}
这是布局图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_teladoimc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.victorsales.testeapp.teladoimc">
<TextView
android:text="@string/ndice_de_massa_corporal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:id="@+id/textView"
android:textAlignment="center"
android:layout_alignParentEnd="true"
android:textSize="24sp"
android:textColor="@android:color/background_dark" />
<TextView
android:text="@string/importante_adultos_abrange_um_grupo_de_pessoas_com_idade_entre_20_e_60_anos_idosos_abrange_um_grupo_de_pessoas_maiores_que_60_anos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/bt7"
android:layout_alignParentStart="true"
android:layout_marginTop="47dp"
android:id="@+id/textView2"
android:textAppearance="@style/MyTitleTextStyle"
android:fontFamily="sans-serif"
android:textAlignment="center" />
<Button
android:text="@string/adulto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="47dp"
android:id="@+id/bt6"
android:background="@color/colorPrimary"
android:textSize="18sp"
android:fontFamily="sans-serif"
android:textStyle="normal|bold"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:onClick="telaadulto" />
<Button
android:text="@string/idoso"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="@+id/bt7"
android:background="@color/colorPrimary"
android:textSize="18sp"
android:fontFamily="sans-serif"
android:textStyle="normal|bold"
android:layout_below="@+id/bt6"
android:layout_alignParentStart="true"
android:onClick="telaidoso" />
</RelativeLayout>
它本来要转到第三个活动,但当我单击按钮时,它将用于应用程序。这将解决问题:
public class teladoimc extends AppCompatActivity implements View.OnClickLister {
Button bt6;
Button bt7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teladoimc);
bt6 = (Button) findViewById(R.id.bt6);
bt7 = (Button) findViewById(R.id.bt7);
bt6.setOnClickListener(this); //you need to add this for clicks to work.
bt7.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.bt6) {
Intent intent = new Intent(teladoimc.this, imcadulto.class);
startActivity(intent);
} else if (v.getId() == R.id.bt7) {
Intent intent = new Intent(teladoimc.this, imcidoso.class);
startActivity(intent);
}
}
}
代码的问题在于,您没有在类中实现视图.OnClickListener
,而是重写了它的onClick
方法。这是必要的:
implements View.OnClickLister
此外,为了使接口正常工作,您首先需要通过view.setOnClickListener(object)
提供接口对象,这里指的是实现view.OnClickListener
的类
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
您必须对bt6和BT7使用
setOnClickListener
,就像对其他布局一样您是否为第二个布局设置了xml格式的onClick
?这就是问题所在,它不会确认错误,应用程序工作正常,但是,当单击第二个活动的按钮时,它不会执行该功能。我已经尝试使用MainActivity中使用的相同代码,但仍然不起作用,我尝试了您的方法,但不起作用:(添加第二个活动的布局代码并更新其他代码。我将布局放在问题描述中,这是您想要的吗?您在onClick中使用的setContentView是什么?