Android 无法设置OnTouchListener

Android 无法设置OnTouchListener,android,ontouchlistener,Android,Ontouchlistener,福克斯,这将是一个愚蠢的错误,但我真的猜不出它在哪里。 我正在尝试设置OnTouchListener,但没有调用onTouch方法 这是我的密码: public class StepActivity extends SherlockActivity { private Recipe recipe = null; private String TAG = "StepActivity"; private ArrayList<Step> step

福克斯,这将是一个愚蠢的错误,但我真的猜不出它在哪里。 我正在尝试设置OnTouchListener,但没有调用onTouch方法

这是我的密码:

    public class StepActivity extends SherlockActivity  
    {
    private Recipe recipe = null;
    private String TAG = "StepActivity";
    private ArrayList<Step> steps;
    private int caret = 0;
    private int currentStep = 1;
    private int stepQuantity;
    private TextView viewStepBody;
    private TextView stepNumber;
    private ImageView viewPicture;
    private String bodyText;
    private String picture;
    private String [] pictures = new String [5];



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.step_page);
        RelativeLayout view = new RelativeLayout(this);
        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motion) {

                Log.i(TAG, "In onTouch");
                float downXValue = 0;
                // Get the action that was done on this touch event
                switch (motion.getAction())
                {

                    case MotionEvent.ACTION_DOWN:
                    {
                        Log.i(TAG, "In onTouch1");
                        // store the X value when the user's finger was pressed down
                        downXValue = motion.getX();
                        break;
                    }

                    case MotionEvent.ACTION_UP:
                    {
                        Log.i(TAG, "In onTouch2");
                        // Get the X value when the user released his/her finger
                        float currentX = motion.getX();            
                        // going backwards: pushing stuff to the right
                        if (downXValue < currentX)
                        {
                            Log.i(TAG, "In onTouch previous");
                            goPrevious();
                        }

                        // going forwards: pushing stuff to the left
                        if (downXValue > currentX)
                        {
                            Log.i(TAG, "In onTouch next");
                            goNext();
                        }
                        break;
                    }
                }
                // if you return false, these actions will not be recorded
                return true;
            }

        });

        viewStepBody = (TextView)findViewById(R.id.step_body);
        stepNumber = (TextView)findViewById(R.id.step_number);
        viewPicture = (ImageView)findViewById(R.id.picture);


        TextView recipeTitle = (TextView)findViewById(R.id.recipe_title);
        try {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            recipe = (Recipe)getIntent().getSerializableExtra("Recipe1");
            steps = recipe.getSteps();
            stepQuantity = steps.size();

            Log.d(TAG,"steps: " + steps.size());
            if (stepQuantity > 0) {
                Step step = steps.get(0);
                pictures[0] = Constants.URL + step.getImg_url();
                bodyText =  step.getInstruction();
                new DownloadImageTask().execute(pictures);
                recipeTitle.setText(recipe.getTitle());
            }
            updateInfo();
        } catch (Exception e) {
            Toast.makeText(this, "Error occured" + e.getMessage(), 200);
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.step_menu, (com.actionbarsherlock.view.Menu) menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void updateInfo() {
        new DownloadImageTask().execute(pictures);
        viewStepBody.setText(bodyText);
        stepNumber.setText(currentStep + "/" + stepQuantity);
    }

    private void goNext() {
        if (currentStep != stepQuantity) {
            caret++;
            currentStep++;
            Step newStep = steps.get(caret);
            pictures[0] = Constants.URL + newStep.getImg_url();
            bodyText = newStep.getInstruction();
            updateInfo();
        } else {
            caret = 0;
            currentStep = 1;
            Step newStep = steps.get(caret);
            bodyText = newStep.getInstruction();
            pictures[0] = Constants.URL + newStep.getImg_url();
            updateInfo();
        }
    }

    private void goPrevious() {
        if (currentStep != 1) {
            caret--;
            currentStep--;
            Step newStep = steps.get(caret);
            bodyText = newStep.getInstruction();
            pictures[0] = Constants.URL + newStep.getImg_url();
            updateInfo();
        }
        else {
            caret = stepQuantity - 1;
            currentStep = stepQuantity;
            Step newStep = steps.get(caret);
            bodyText = newStep.getInstruction();
            pictures[0] = Constants.URL + newStep.getImg_url();
            updateInfo();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())  {
        case R.id.next: 
            goNext();
            return true;
        case R.id.previous: 
            goPrevious();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
公共类StepActivity扩展了SherlockActivity
{
私有配方=空;
私有字符串TAG=“StepActivity”;
私有数组列表步骤;
私有整数插入符=0;
私有int currentStep=1;
私人数量;
私有文本视图视图步骤体;
私有文本视图步骤号;
私人影像查看图片;
私有字符串正文;
私有字符串图片;
私有字符串[]图片=新字符串[5];
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.step_页面);
RelativeLayout视图=新的RelativeLayout(此);
view.setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件、运动){
Log.i(标签“In-onTouch”);
浮动向下xvalue=0;
//获取在此触摸事件上执行的操作
开关(motion.getAction())
{
case MotionEvent.ACTION\u DOWN:
{
Log.i(标签“In-onTouch1”);
//当按下用户的手指时,存储X值
downXValue=motion.getX();
打破
}
case MotionEvent.ACTION\u UP:
{
Log.i(标签“In-onTouch2”);
//获取用户释放手指时的X值
float currentX=motion.getX();
//倒退:把东西往右边推
如果(向下X值<当前X)
{
Log.i(标记为“在onTouch previous中”);
goPrevious();
}
//向前:把东西推到左边
如果(向下X值>当前X)
{
Log.i(标记“在onTouch下一步”);
goNext();
}
打破
}
}
//如果返回false,则不会记录这些操作
返回true;
}
});
viewStepBody=(TextView)findViewById(R.id.step_body);
stepNumber=(TextView)findViewById(R.id.step_编号);
viewPicture=(ImageView)findViewById(R.id.picture);
TextView recipeTitle=(TextView)findViewById(R.id.recipe\u title);
试一试{
getSupportActionBar().setDisplayShowTitleEnabled(false);
配方=(配方)getIntent().getSerializableExtra(“Recipe1”);
步骤=recipe.getSteps();
stepQuantity=steps.size();
Log.d(标记“steps:+steps.size());
如果(步骤数量>0){
步骤=步骤。获取(0);
图片[0]=Constants.URL+step.getImg_URL();
bodyText=step.getInstruction();
新建下载ImageTask()。执行(图片);
recipeTitle.setText(recipe.getTitle());
}
updateInfo();
}捕获(例外e){
Toast.makeText(此“错误发生”+e.getMessage(),200);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
com.actionbarsherlock.view.MenuInflater充气器=getSupportMenuInflater();
充气器。充气(R.menu.step_菜单,(com.actionbarsherlock.view.menu)菜单);
返回super.onCreateOptions菜单(菜单);
}
私有void updateInfo(){
新建下载ImageTask()。执行(图片);
viewStepBody.setText(bodyText);
stepNumber.setText(当前步骤+“/”+步骤数量);
}
私有void goNext(){
如果(当前步数!=步数){
插入符号++;
currentStep++;
步骤newStep=steps.get(插入符号);
图片[0]=Constants.URL+newStep.getImg_URL();
bodyText=newStep.getInstruction();
updateInfo();
}否则{
插入符号=0;
currentStep=1;
步骤newStep=steps.get(插入符号);
bodyText=newStep.getInstruction();
图片[0]=Constants.URL+newStep.getImg_URL();
updateInfo();
}
}
私人文件{
如果(当前步骤!=1){
插入符号--;
当前步骤--;
步骤newStep=steps.get(插入符号);
bodyText=newStep.getInstruction();
图片[0]=Constants.URL+newStep.getImg_URL();
updateInfo();
}
否则{
插入符号=步骤数量-1;
currentStep=步进量;
步骤newStep=steps.get(插入符号);
bodyText=newStep.getInstruction();
图片[0]=Constants.URL+newStep.getImg_URL();
updateInfo();
}
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.next:
goNext();
返回true;
案例R.id.先前:
goPrevious();
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
下面是我的step_page.xml,它传递给setContentView()方法


这里建议的另一种方法不起作用:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.step_page);
    RelativeLayout view = (RelativeLayout) findViewById(R.id.step_layout1);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motion) {

            Log.i(TAG, "In onTouch");
            float downXValue = 0;
            // Get the action that was done on this touch event
            switch (motion.getAction())
            {

                case MotionEvent.ACTION_DOWN:
                {
                    Log.i(TAG, "In onTouch1");
                    // store the X value when the user's finger was pressed down
                    downXValue = motion.getX();
                    break;
                }

                case MotionEvent.ACTION_UP:
                {
                    Log.i(TAG, "In onTouch2");
                    // Get the X value when the user released his/her finger
                    float currentX = motion.getX();            
                    // going backwards: pushing stuff to the right
                    if (downXValue < currentX)
                    {
                        Log.i(TAG, "In onTouch previous");
                        goPrevious();
                    }

                    // going forwards: pushing stuff to the left
                    if (downXValue > currentX)
                    {
                        Log.i(TAG, "In onTouch next");
                        goNext();
                    }
                    break;
                }
            }
            // if you return false, these actions will not be recorded
            return true;
        }

    });
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.step_页面);
RelativeLayout视图=(RelativeLayout)findViewById(R.i
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.step_page);
    RelativeLayout view = (RelativeLayout) findViewById(R.id.step_layout1);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motion) {

            Log.i(TAG, "In onTouch");
            float downXValue = 0;
            // Get the action that was done on this touch event
            switch (motion.getAction())
            {

                case MotionEvent.ACTION_DOWN:
                {
                    Log.i(TAG, "In onTouch1");
                    // store the X value when the user's finger was pressed down
                    downXValue = motion.getX();
                    break;
                }

                case MotionEvent.ACTION_UP:
                {
                    Log.i(TAG, "In onTouch2");
                    // Get the X value when the user released his/her finger
                    float currentX = motion.getX();            
                    // going backwards: pushing stuff to the right
                    if (downXValue < currentX)
                    {
                        Log.i(TAG, "In onTouch previous");
                        goPrevious();
                    }

                    // going forwards: pushing stuff to the left
                    if (downXValue > currentX)
                    {
                        Log.i(TAG, "In onTouch next");
                        goNext();
                    }
                    break;
                }
            }
            // if you return false, these actions will not be recorded
            return true;
        }

    });