js091514 js091514 - 5 months ago 18
Android Question

EditText OnClick Exception

So I am making an app for an assignment.

Basically convert Miles to KM or KM to Miles.

I've now got the radio buttons working but when I add the onclick event, upon clicking the edittext fields I get an exception. :

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


Here is the code

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

}


The layout

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

Answer

You are specifying the onClick event method through XML, and it looks like this does not work with support library AppCompatActivity.

You should just get the reference to the EditText and call 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
    });
}
Comments