App Crashes and does not load on emulator nor physical device(logs in post)

+1 Nelson Hernandez · December 21, 2015
Hi, I've been trying to complete the assignment given during lesson 21, but every time I try to run my app, it does not work. Here is the source code and the logs: 



package com.android.example.assignment2;

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


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

private TextView myTextView;
private GestureDetectorCompat gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button myButton = (Button)findViewById(R.id.myButton);

myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView textView1 = (TextView) findViewById(R.id.myTextView);
textView1.setText("I got tapped");
}
}
);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
//////////////////// Start Gestures /////////////////////


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

@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return true;
}

@Override
public boolean onDown(MotionEvent e) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
myTextView.setText("I got scrolled");
return true;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
myTextView.setText("I got flinged");
return true;
}

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




LOGS:

12-19 23:28:24.234 20487-20487/com.android.example.assignment2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-19 23:28:24.254 20487-20487/com.android.example.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 java.lang.NullPointerException
                                                                                     at com.android.example.assignment2.MainActivity.onScroll(MainActivity.java:80)
                                                                                     at android.support.v4.view.GestureDetectorCompat$GestureDetectorCompatImplBase.onTouchEvent(GestureDetectorCompat.java:336)
                                                                                     at android.support.v4.view.GestureDetectorCompat.onTouchEvent(GestureDetectorCompat.java:538)
                                                                                     at com.android.example.assignment2.MainActivity.onTouchEvent(MainActivity.java:45)
                                                                                     at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
                                                                                     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
                                                                                     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
                                                                                     at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                     at android.os.Looper.loop(Looper.java:123)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:3683)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:507)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
                                                                                     at dalvik.system.NativeStart.main(Native Method)

This has started to frustrate me. Help would be greatly appreciated.


Post a Reply

Replies

Oldest  Newest  Rating
0 Nelson Hernandez · December 22, 2015
Thank you very much for your insight on this problem.
0 rabbydude RABBY · December 23, 2015
Thanks gary whitney (Y)
0 Gary Whitney · December 21, 2015
It says the problem is at line 80.
turn on the show line numbers option and then  look at line 80.

Without counting lines, I would say it is this line with the double this looks suspicious.
this.gestureDetector = new GestureDetectorCompat(this, this);

P.S.
The gestures were suppose to be applied to the screen and I think your are applying it to a button.
  • 1

Java / Android Development

107,328 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator