Reputation: 27
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.widget.TextView;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private TextView buckysmesage;
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buckysmesage = (TextView) findViewById(R.id.textView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
setContentView(R.layout.activity_main);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
buckysmesage.setText("onDoubleTap");
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
buckysmesage.setText("onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
buckysmesage.setText("onDoubleTapEvent");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
buckysmesage.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
buckysmesage.setText("OnShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
buckysmesage.setText("onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
buckysmesage.setText("onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
buckysmesage.setText("OnLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
buckysmesage.setText("ONFLING");
return true;
}
@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);
}
}
I have no idea why my app is not running, I was doing a tutorial on android gestures, basically what the app is supposed to do is what ever gesture the user inputs, the name of it gets shown in the textbox (buckysmessage). Every time I load it onto my emulator and a perform a gesture the app simply crashes.If anybody could go through my code and help me that would be absolutely splendid.
Upvotes: 0
Views: 110
Reputation: 11642
We should add setContentView() before getting the object from layout, the problem is when you try to do some action your gesture methods will call, and you are trying to show the message in textview,but your textview is null
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buckysmesage = (TextView) findViewById(R.id.textView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
}
Upvotes: 0
Reputation: 7070
Set your layout first before using the views in the layout-
Add setContentView(R.layout.activity_main);
immediately after super.onCreate(savedInstanceState);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buckysmesage = (TextView) findViewById(R.id.textView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
}
Upvotes: 1