Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol KeyEvent

Tags:

java

android

I really cannot find the answer for this problem on the internet. I am trying to navigate through XML items via the Android volume buttons. StackOverflow gave me the following code to do so:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_DOWN) {
            //TODO
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            //TODO
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

But when I try to paste this piece of code in my MainActivity.java, 'KeyEvent' turns red with the error 'Cannot resolve symbol KeyEvent'. This is the code from MainActivity.java

import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.InputEvent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {

        ListView lvPcsPost;
        ArrayList<PostValue> postValueArrayList;

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

            lvPcsPost = (ListView) findViewById(R.id.lvPcsPost);
            lvPcsPost.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (postValueArrayList != null && postValueArrayList.size() > 0) {
                        Intent intentShowPost = new Intent(Intent.ACTION_VIEW, Uri.parse(postValueArrayList.get(position).getVid()));
                        startActivity(intentShowPost);
                    }
                }
            });
            new PostAsync().execute();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    //TODO
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {
                    //TODO
                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
        }
    }


class PostAsync extends AsyncTask<Void, Void, Void> {
        ProgressDialog pd;
        XMLHelper helper;


        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(MainActivity.this, "Please wait...", "Loading", true, false);
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            helper = new XMLHelper();
            helper.get();
            postValueArrayList = helper.getPostsList();
            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            PostBaseAdapter postBaseAdapter = new PostBaseAdapter(MainActivity.this, postValueArrayList);
            lvPcsPost.setAdapter(postBaseAdapter);
            pd.dismiss();
        }


    }

}

Anyone who sees the problem here?

like image 229
Jan Hoi Avatar asked Mar 07 '26 15:03

Jan Hoi


1 Answers

You need to import the class KeyEvent:

import android.view.KeyEvent;
like image 83
M A Avatar answered Mar 10 '26 06:03

M A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!