当声明公共类MainActivity扩展Activity时,实现OnClickListener'

时间:2014-11-20 11:37:28

标签: java android

MainActivity突出显示并显示错误'嵌套类型MainActivity无法隐藏封闭类型'任何帮助解决此问题将不胜感激。当我删除第一个MainActivity时,其余代码会出现错误?

package com.example.myfirstapp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public class MainActivity extends Activity implements OnClickListener {

    EditText fuelCost;
    EditText distance;
    EditText milesPerGallon;

    Button btn_1;

    TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        fuelCost = (EditText) findViewById(R.id.fuelCost);
        distance = (EditText) findViewById(R.id.distance);
        milesPerGallon = (EditText) findViewById(R.id.milesPerGallon);

        btn_1 = (Button) findViewById(R.id.btn_1);

        result =(TextView) findViewById(R.id.result);

        btn_1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        float num1 = 0;
        float num2 = 0;
        float num3 = 0;

        float result = 0;

        if (TextUtils.isEmpty(fuelCost.getText().toString())
                || TextUtils.isEmpty(distance.getText().toString())
                || TextUtils.isEmpty(milesPerGallon.getText().toString())) {
            return;
        }

        num1 = Float.parseFloat(fuelCost.getText().toString());
        num2 = Float.parseFloat(distance.getText().toString());
        num3 = Folat.parseFloat(milesPerGallon.getText().toString());

        switch (v.getId()) {
        case R.id.btn_1:
            result = ((num1 * 4.5461) * (num2 / num3)) / 100;
            break;
        default:
            break;
        }

        result.setText("£" + result);
    }       
}

}

2 个答案:

答案 0 :(得分:0)

您可能在相同活动文件中声明主要活动两次。 只需删除错误的声明。

答案 1 :(得分:0)

确定这很简单,但我认为你是编程新手,所以其他人希望你做的事可以从下面的代码中看到:

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

    public class MainActivity extends Activity implements View.OnClickListener {

        EditText mFuelCost;
        EditText mDistance;
        EditText mMilesPerGallon;

        Button mBtn1;

        TextView mResult;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            mFuelCost = (EditText) findViewById(R.id.fuelCost);
            mDistance = (EditText) findViewById(R.id.distance);
            mMilesPerGallon = (EditText) findViewById(R.id.milesPerGallon);

            mBtn1 = (Button) findViewById(R.id.btn_1);

            mResult =(TextView) findViewById(R.id.result);

            mBtn1.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            float num1 = 0;
            float num2 = 0;
            float num3 = 0;

            float result = 0;

            if (TextUtils.isEmpty(mFuelCost.getText().toString())
                    || TextUtils.isEmpty(mDistance.getText().toString())
                    || TextUtils.isEmpty(mMilesPerGallon.getText().toString())) {
                return;
            }

            num1 = Float.parseFloat(mFuelCost.getText().toString());
            num2 = Float.parseFloat(mDistance.getText().toString());
            num3 = Float.parseFloat(mMilesPerGallon.getText().toString());

            switch (v.getId()) {
                case R.id.btn_1:
                    result = ((num1 * 4.5461f) * (num2 / num3)) / 100;
                    break;
                default:
                    break;
            }

            mResult.setText("£" + result);
        }
    }