Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

android - Cancelling event due to no window focus : MotionEvent

Here's the story:

I am trying to build a guessing game in Android Studio. Where the user has to guess a number that resides between 0 and 1000. Every time the user makes a guess, the computer will tell you if the guess is smaller than the actual number(Cold) or greater than the actual number(hot). I decided to have the user enter his guess inside of an editTextView. I binded an onFocus event listener to the editTextView so that when the user presses enter or taps somewhere else, it submits the guess to the computer. I ran the code in the emulator and I recieved some messages from the console say things similar to the following:

W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=605.52246, y[0]=969.4336, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=238238, downTime=235422, deviceId=0, source=0x1002 }

public class GameActivity extends AppCompatActivity implements View.OnFocusChangeListener {
    public final int MAXNUMBER = 1000;
    private int theAnswer; // stores the number the user has to guess
    public int guesses = 0;
    public EditText userGuess;
    public TextView hotOrCold;
    public String isHotOrCold(int userInput){
        return theAnswer < userInput?"Hot":"Cold"; // Utility to tell the user if his guess is greater than or less than the number.
    }
    public boolean isValidInput(int num){
        return num >= 0 && num <= MAXNUMBER;
    }
    public void resetGame(){
        guesses = 0;
        theAnswer = (int) (Math.random() * (MAXNUMBER + 1));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        userGuess = (EditText)findViewById(R.id.userGuess);
        hotOrCold = (TextView) findViewById(R.id.hotOrCold);
        resetGame();
        userGuess.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        /* When focus is lost check that the text field
        * has valid values.
        */
        Log.i("","RAN!");
        if (!hasFocus) {
            guesses++;
            int userInput = Integer.valueOf(userGuess.getText().toString()); // get user input
            Log.i("userInput is: ",userInput+"");
            if (isValidInput(userInput)){
                if (theAnswer == userInput){
                    Toast.makeText(getApplicationContext(),"Congrats! The number was: "+userInput+". You got it in " + guesses + " guesses!",Toast.LENGTH_LONG); // Tell user he got it right
                    hotOrCold.setText("Hot Or Cold"); // set text to default
                    resetGame();
                } else {
                    hotOrCold.setText(isHotOrCold(userInput)); // Tells user if he is hot or cold.
                }
            } else {
                // do nothing
            }
        } else {
            Log.i("Did Run","");
        }
    }
}

This is the code that produces the message. If you require any more information or would like me to elaborate on something, please let me know!

Assume Ids are correct, and all views that are in the program exist in the activity. It seems like the function onFocusChange is not running whenever the user presses enter or changes the focus.

EDIT: I have a new error that showed up when I turned on the focusable property on the edittext element. here is the error:

W/ResourceType: Failure getting entry for 0x01080346 (t=7 e=838) (error -75)
    [ 07-13 12:54:05.572  2725: 2725 I/         ]
    RAN!

It only happens ONCE at the start of the program

The following code provides the activity that the code links to.

<?xml version="1.0" encoding="utf-8"?>
<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: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.alexander.guessinggame.GameActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hot or Cold"
        android:id="@+id/hotOrCold"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textIsSelectable="false"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="*Hot:too big, cold:too small"
        android:id="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textIsSelectable="false"
        android:textSize="17sp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/userGuess"
        android:layout_above="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="111dp"
        android:numeric="integer"
        android:selectAllOnFocus="true"
        android:editable="true"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:enabled="true"
        android:singleLine="true"
        android:clickable="true"
        android:maxLength="4" />

</RelativeLayout>

If there is anything that you need do not hesitate to ask me!

Can anyone tell me what the following errors mean? They come out when I click on the edittext box.

  1. W/EGL_emulation: eglSurfaceAttrib not implemented
  2. W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa9fc6300, error=EGL_SUCCESS
  3. E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae3f3fb0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can override:

onKeyUp(int keyCode, KeyEvent event)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...