Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击“侦听器”不会切换活动_Java_Android_Android Intent_Android Activity_Onclicklistener - Fatal编程技术网

Java 单击“侦听器”不会切换活动

Java 单击“侦听器”不会切换活动,java,android,android-intent,android-activity,onclicklistener,Java,Android,Android Intent,Android Activity,Onclicklistener,在android开发中,需要一些从活动切换到活动的帮助。我正在尝试从主活动转到主菜单活动。我实现了一个onclicklistener来完成这项工作。当我在模拟器上运行应用程序时,它运行良好,但是当我单击用onclicklistener实现的按钮时,它什么也不做 package com.example.bmiworking; import android.app.Activity; import android.content.DialogInterface; import android.co

在android开发中,需要一些从活动切换到活动的帮助。我正在尝试从主活动转到主菜单活动。我实现了一个onclicklistener来完成这项工作。当我在模拟器上运行应用程序时,它运行良好,但是当我单击用onclicklistener实现的按钮时,它什么也不做

package com.example.bmiworking;

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






            public class MainActivity extends Activity implements OnClickListener{
            /** Called when the activity is first created. */
            Button btn;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                 btn = (Button)findViewById(R.id.homeClickHandler);
            }
            public void onClick(View v){
                if(v.getId() == R.id.homeClickHandler){
                startActivity(new Intent(this,MainMenu.class));
                }
            }


            public void calculateClickHandler(View view) {
             // make sure we handle the click of the calculator button

             if (view.getId() == R.id.calculateButton) {

              // get the references to the widgets
              EditText weightText = (EditText)findViewById(R.id.weightText);
              EditText heightText = (EditText)findViewById(R.id.heightText);
              TextView resultText = (TextView)findViewById(R.id.resultLabel);

              // get the users values from the widget references

              float weight = Float.parseFloat(weightText.getText().toString());
              float height = Float.parseFloat(heightText.getText().toString());

              // calculate the bmi value

              float bmiValue = calculateBMI(weight, height);

              // interpret the meaning of the bmi value
              String bmiInterpretation = interpretBMI(bmiValue);

              // now set the value in the result text

              resultText.setText(bmiValue + "-" + bmiInterpretation);
             }
            }

            // the formula to calculate the BMI index

            // check for http://en.wikipedia.org/wiki/Body_mass_index
            private float calculateBMI (float weight, float height) {

             return (float) (weight * 4.88 / (height * height));
            }


            // interpret what BMI means
            private String interpretBMI(float bmiValue) {

             if (bmiValue < 16) {
              return "Severely Underweight - See Weight Gain";
             } else if (bmiValue < 18.5) {

              return "Underweight - See Weight Gain";
             } else if (bmiValue < 25) {

              return "Normal - No Recomendations";
             } else if (bmiValue < 30) {

              return "Overweight - See Weight Loss";
             } else {
              return "Obese - See Weight Loss";
             }

            }


            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        }



     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="BMI Calculator"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/weightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:inputType="numberDecimal" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:text="@string/weightLabel"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/weightText"
            android:layout_centerHorizontal="true"
            android:text="@string/heightLabel"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/heightText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/weightText"
            android:layout_below="@+id/textView3"
            android:ems="10"
            android:inputType="numberDecimal" />

        <Button
            android:id="@+id/calculateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/heightText"
            android:layout_centerHorizontal="true"
            android:onClick="calculateClickHandler"
            android:text="@string/calculateButton" />

        <TextView
            android:id="@+id/resultLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/calculateButton"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="15dp"
            android:text="@string/emptyString"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/homeClickHandler"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@string/homeButton" />

    </RelativeLayout>

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.bmiworking"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.bmiworking.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.bmiworking.MainMenu"
                android:label="@string/app_name" >
                </activity>
        </application>

    </manifest>
package com.example.bmiworking;
导入android.app.Activity;
导入android.content.DialogInterface;
导入android.content.DialogInterface.OnClickListener;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类MainActivity扩展活动实现OnClickListener{
/**在首次创建活动时调用*/
按钮btn;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(按钮)findViewById(R.id.homeClickHandler);
}
公共void onClick(视图v){
if(v.getId()==R.id.homeClickHandler){
startActivity(新意图(这个,MainMenu.class));
}
}
public void calculateClickHandler(视图){
//确保我们处理计算器按钮的点击
if(view.getId()==R.id.calculateButton){
//获取小部件的引用
EditText-weightText=(EditText)findViewById(R.id.weightText);
EditText heightText=(EditText)findViewById(R.id.heightText);
TextView resultText=(TextView)findViewById(R.id.resultLabel);
//从小部件引用获取用户值
float-weight=float.parseFloat(weightText.getText().toString());
float height=float.parseFloat(heightText.getText().toString());
//计算bmi值
浮动BMI值=计算BMI(体重、身高);
//解释bmi值的含义
字符串bmi解释=解释bmi(bmi值);
//现在在结果文本中设置值
resultText.setText(bmiValue+“-”+BMI解释);
}
}
//计算体重指数的公式
//查证http://en.wikipedia.org/wiki/Body_mass_index
专用浮子计算器BMI(浮子重量、浮子高度){
返回(浮动)(重量*4.88/(高度*高度));
}
//解读体重指数的含义
专用字符串解释BMI(浮点BMI值){
如果(BMI值<16){
返回“严重体重不足-参见体重增加”;
}否则如果(BMI值<18.5){
返回“体重不足-参见体重增加”;
}否则如果(BMI值<25){
返回“正常-无建议”;
}否则如果(BMI值<30){
返回“超重-参见体重减轻”;
}否则{
返回“肥胖-见体重减轻”;
}
}
@凌驾
公共void onClick(对话框接口arg0,int arg1){
//TODO自动生成的方法存根
}
}

您导入了错误的onClick Listener,因此请删除该导入,同时删除DialogInterface onClick方法

import android.view.View.OnClickListener;
下面是“btn=(按钮)findViewById(R.id.homeClickHandler);”和:

完成:

package com.example.bmiworking;

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

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.homeClickHandler);
    btn.setOnClickListener(this);
}

    @Override
public void onClick(View v) {
    if (v.getId() == R.id.homeClickHandler) {
        startActivity(new Intent(this, MainMenu.class));
    }
}

public void calculateClickHandler(View view) {
    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.calculateButton) {

        // get the references to the widgets
        EditText weightText = (EditText) findViewById(R.id.weightText);
        EditText heightText = (EditText) findViewById(R.id.heightText);
        TextView resultText = (TextView) findViewById(R.id.resultLabel);

        // get the users values from the widget references

        float weight = Float.parseFloat(weightText.getText().toString());
        float height = Float.parseFloat(heightText.getText().toString());

        // calculate the bmi value

        float bmiValue = calculateBMI(weight, height);

        // interpret the meaning of the bmi value
        String bmiInterpretation = interpretBMI(bmiValue);

        // now set the value in the result text

        resultText.setText(bmiValue + "-" + bmiInterpretation);
    }
}

// the formula to calculate the BMI index

// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI(float weight, float height) {

    return (float) (weight * 4.88 / (height * height));
}

// interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Severely Underweight - See Weight Gain";
    } else if (bmiValue < 18.5) {

        return "Underweight - See Weight Gain";
    } else if (bmiValue < 25) {

        return "Normal - No Recomendations";
    } else if (bmiValue < 30) {

        return "Overweight - See Weight Loss";
    } else {
        return "Obese - See Weight Loss";
    }

}

}
package com.example.bmiworking;
导入android.app.Activity;
导入android.content.DialogInterface;
导入android.view.view.OnClickListener;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类MainActivity扩展活动实现OnClickListener{
/**在首次创建活动时调用*/
按钮btn;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(按钮)findViewById(R.id.homeClickHandler);
btn.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.homeClickHandler){
startActivity(新意图(这个,MainMenu.class));
}
}
public void calculateClickHandler(视图){
//确保我们处理计算器按钮的点击
if(view.getId()==R.id.calculateButton){
//获取小部件的引用
EditText-weightText=(EditText)findViewById(R.id.weightText);
EditText heightText=(EditText)findViewById(R.id.heightText);
TextView resultText=(TextView)findViewById(R.id.resultLabel);
//从小部件引用获取用户值
float-weight=float.parseFloat(weightText.getText().toString());
float height=float.parseFloat(heightText.getText().toString());
//计算bmi值
浮动BMI值=计算BMI(体重、身高);
//解释bmi值的含义
字符串bmi解释=解释bmi(bmi值);
//现在在结果文本中设置值
resultText.setText(bmiValue+“-”+BMI解释);
}
}
//计算体重指数的公式
//查证http://en.wikipedia.org/wiki/Body_mass_index
专用浮子计算器BMI(浮子重量、浮子高度){
返回(浮动)(重量*4.88/(高度*高度));
}
//解读体重指数的含义
私人字符串i
package com.example.bmiworking;

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

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.homeClickHandler);
    btn.setOnClickListener(this);
}

    @Override
public void onClick(View v) {
    if (v.getId() == R.id.homeClickHandler) {
        startActivity(new Intent(this, MainMenu.class));
    }
}

public void calculateClickHandler(View view) {
    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.calculateButton) {

        // get the references to the widgets
        EditText weightText = (EditText) findViewById(R.id.weightText);
        EditText heightText = (EditText) findViewById(R.id.heightText);
        TextView resultText = (TextView) findViewById(R.id.resultLabel);

        // get the users values from the widget references

        float weight = Float.parseFloat(weightText.getText().toString());
        float height = Float.parseFloat(heightText.getText().toString());

        // calculate the bmi value

        float bmiValue = calculateBMI(weight, height);

        // interpret the meaning of the bmi value
        String bmiInterpretation = interpretBMI(bmiValue);

        // now set the value in the result text

        resultText.setText(bmiValue + "-" + bmiInterpretation);
    }
}

// the formula to calculate the BMI index

// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI(float weight, float height) {

    return (float) (weight * 4.88 / (height * height));
}

// interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Severely Underweight - See Weight Gain";
    } else if (bmiValue < 18.5) {

        return "Underweight - See Weight Gain";
    } else if (bmiValue < 25) {

        return "Normal - No Recomendations";
    } else if (bmiValue < 30) {

        return "Overweight - See Weight Loss";
    } else {
        return "Obese - See Weight Loss";
    }

}

}