EditText OnClick异常

时间:2015-09-22 03:55:33

标签: java android android-edittext

所以我正在为作业制作应用程序。

基本上将里程转换为KM或KM转换为里程。

我现在让单选按钮工作但是当我添加onclick事件时,单击edittext字段后我得到一个例外。 :

java.lang.IllegalArgumentException: 
          Expected receiver of type a00891437.set3s.comp3975.convertunits.MainActivity, 
          but got android.support.v7.internal.widget.TintContextWrapper  

这是代码

    package a00891437.set3s.comp3975.convertunits;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;



public class MainActivity extends AppCompatActivity {

    @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.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void onRadioButtonClicked(final View view) {
        View mText = findViewById(R.id.MilesText);
        View kmText = findViewById(R.id.KilometersText);

        if (view.getId() == R.id.KmToMButton) {
            mText.setEnabled(false);
            kmText.setEnabled(true);

        }
        if (view.getId() == R.id.MToKmButton) {
            kmText.setEnabled(false);
            mText.setEnabled(true);
        }
    }

    public void onClickedText(final View view) {
        Log.d("MainActivity", "Clicked");
    }

}

布局

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/screen">

    TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="110dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/KilometersText"
        android:layout_above="@+id/button"
        android:layout_marginBottom="134dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:enabled="false"
        android:onClick="onClickedText"
       />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/MilesText"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/KilometersText"
        android:layout_alignStart="@+id/KilometersText"
        android:enabled="false"
        android:onClick="onClickedText"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Km"
        android:id="@+id/Kilometers"
        android:layout_alignTop="@+id/KilometersText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="43dp"
        android:layout_marginStart="43dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="M"
        android:id="@+id/Miles"
        android:layout_alignTop="@+id/MilesText"
        android:layout_alignRight="@+id/Kilometers"
        android:layout_alignEnd="@+id/Kilometers" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kilometers to Miles"
            android:id="@+id/KmToMButton"
            android:layout_alignParentTop="true"
            android:layout_alignLeft="@+id/MToKmButton"
            android:layout_alignStart="@+id/MToKmButton"
            android:checked="false"
            android:onClick="onRadioButtonClicked"/>/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Miles to Kilometers"
            android:id="@+id/MToKmButton"
            android:layout_below="@+id/KmToMButton"
            android:layout_alignLeft="@+id/Kilometers"
            android:layout_alignStart="@+id/Kilometers"
            android:checked="false"
            android:onClick="onRadioButtonClicked"/>/>
    </RadioGroup>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:5)

您正在通过XML指定onClick事件方法,看起来这不适用于支持库AppCompatActivity

您应该获得EditText的引用并致电setOnClickListener()

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

    EditText kilometers = (EditText) findViewById(R.id.KilometersText);
    kilometers.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onClickedText(v);
        }
    });

    // repeat this for the miles EditText

    // you'll need to add an id for the radio group for this to work:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    radioGroup.setOnCheckedChangedListener(new RadioGroup.OnCheckedChangedListener() {

        // I leave the code inside here for you to do as an exercise
    });
}