错误:不兼容的类型:在AndroidStudio中无法将TextView转换为GestureDetector

时间:2015-10-02 08:05:08

标签: java android

package com.example.pranesh.swiperdiaper

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;



public class MainActivity extends ActionBarActivity implements 
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{

private GestureDetector akashMessage;
private GestureDetectorCompat gestureDetector;

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

    akashMessage = (TextView)findViewById(R.id.akashMessage);
    this.gestureDetector=new GestureDetectorCompat(this,this);
    gestureDetector.setOnDoubleTapListener(this);
}
//gesture begins//
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
    akashMessage.setText("onSingleTapConfirmed");
    return true;
}

@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
    akashMessage.setText("onDoubleTap");
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
    akashMessage.setText("onDoubleTapEvent");
    return true;
}

@Override
public boolean onDown(MotionEvent motionEvent) {
    akashMessage.setText("onDown");
    return true;
}

@Override
public void onShowPress(MotionEvent motionEvent) {

}

@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
    akashMessage.setText("onSingleTapUp");
    return true;
}

@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
    akashMessage.setText("onScroll");
    return true;
}

@Override
public void onLongPress(MotionEvent motionEvent) {

}

@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
    akashMessage.setText("onFling");
    return true;
}

//gesture ends//


@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

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

这是我简单的Android应用程序。 问题是我开始了 错误:不兼容的类型:TextView无法转换为GestureDetector 以及 错误:找不到符号方法setText(String)

请帮我找到我的错误 谢谢。

2 个答案:

答案 0 :(得分:3)

logcat抛出什么

  

错误:不兼容的类型:TextView无法转换为   GestureDetector以及错误:找不到符号方法   的setText(字符串)

private GestureDetector akashMessage;

DO private TextView akashMessage;

传递TextView Not GestureDetector。只需替换它

答案 1 :(得分:1)

您已将akashMessage声明为GestureDetector 稍后您尝试为其分配TextView。 这就是你得到错误的原因 " TextView无法转换为GestureDetector"。

private GestureDetector akashMessage;更改为private TextView akashMessage;
并检查xml文件中的ID。