Android - Custom GridView with fling - error

1k views Asked by At

I have I problem with my custom GridView. I wrote the custom GridViewClass to enable the fling gesture within the GridView. However, there are some errors I don't quite understand.

My code:

<pl.flamis.GestureGridView
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            android:numColumns="7"
            android:id="@+id/calendar" 
            android:listSelector="#00000000" 
            android:layoutAnimation="@anim/wave_scale"
            android:background="#c5c5c7" >
</pl.flamis.GestureGridView>

the custom GridView class:

public class GestureGridView extends GridView {

private GestureDetector gd;



public GestureGridView(final Context context, AttributeSet attrs) {
    super(context);
    gd = new GestureDetector(context,
                    new GestureDetector.OnGestureListener() {

                            @Override
                            public boolean onSingleTapUp(MotionEvent e) {
                                    // TODO Auto-generated method stub
                                    return false;
                            }

                            @Override
                            public void onShowPress(MotionEvent e) {
                                    // TODO Auto-generated method stub

                            }

                            @Override
                            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                                            float distanceY) {
                                    // TODO Auto-generated method stub
                                    return false;
                            }

                            @Override
                            public void onLongPress(MotionEvent e) {
                                    // TODO Auto-generated method stub

                            }

                            @Override
                            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                            float velocityY) {
                                    return false;
                            }

                            @Override
                            public boolean onDown(MotionEvent e) {
                                    // TODO Auto-generated method stub
                                    return false;
                            }
                    });
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    gd.onTouchEvent(event);
    return true;
}

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    return gd.onTouchEvent(ev); 
}

}

the main class code:

public class Kalendarz extends Activity implements OnGestureListener{

//Grid data
public static GestureGridView calendar;
private static CellAdapter cell_adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    myGesture = new GestureDetector(this);

    context = getApplicationContext();

    res = getResources();

    Notes = new Notes();
    tble = new Timetable(Notes);

    MDate = new MonthData(tble.getDay(), tble.schift_sys );


    setContentView(R.layout.kalendarz);

  if(tble.schift_sys == -1)
    showDialog(sys_zmian);

  Button next = (Button) findViewById(R.id.next_month);
  next.setOnClickListener(
      new OnClickListener(){
          @Override
          public void onClick(View v) {
              tble.goNextMonth();

              updateDateInfo();

              for(int i=0; i< calendar.getChildCount(); i++){
                  CellButton b = ((CellButton)((LinearLayout)calendar.getChildAt(i)).getChildAt(0));
                  if(i >= tble.getFday()-1 && i < tble.getFday() -1 + tble.getNodays()){
                      b.setText(String.valueOf(i-tble.getFday()+2));
                  }else{
                      b.setText("-1");
                  }
              }

              findViewById(R.id.calendar).invalidate();

              ((GestureGridView)findViewById(R.id.calendar)).startLayoutAnimation();
          }
      }
  );

  Button prev = (Button) findViewById(R.id.previous_month);
  prev.setOnClickListener(
      new OnClickListener(){
          @Override
          public void onClick(View v) {
              tble.goPrevMonth();

              updateDateInfo();

              for(int i=0; i< calendar.getChildCount(); i++){
                  CellButton b = ((CellButton)((LinearLayout)calendar.getChildAt(i)).getChildAt(0));
                  if(i >= tble.getFday()-1 && i < tble.getFday() -1 + tble.getNodays()){
                      b.setText(String.valueOf(i-tble.getFday()+2));
                  }else{
                      b.setText("-1");
                  }
              }

              findViewById(R.id.calendar).invalidate();

              ((GestureGridView)findViewById(R.id.calendar)).startLayoutAnimation();
          }
      }
  );


  GridView day_names = (GridView)findViewById(R.id.day_names);
  day_names.setAdapter( new ArrayAdapter <CharSequence> (
                                this, 
                                R.layout.day_names_element,
                                res.getStringArray(R.array.days)
                            )
                     );


  calendar = (GestureGridView)findViewById(R.id.calendar);
  cell_adapter = new CellAdapter(this);
  calendar.setAdapter(cell_adapter);

  updateDateInfo();

  /*Intent j = new Intent(this, Service.class);
  startService(j);*/
}

public class CellAdapter extends BaseAdapter{

    Context MyContext;

    public CellAdapter(Context _MyContext) {
        MyContext = _MyContext; 
    }

    @Override
    public int getCount() {
        return 6*7;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View MyView = convertView;
         if ( convertView == null ){
            //Inflate the layout
            LayoutInflater li = getLayoutInflater();
            MyView = li.inflate(R.layout.cell_calendar_element, null);

            Button b = (Button)MyView.findViewById(R.id.cellbutton);
            registerForContextMenu(b);

         }

         Button b = (Button)MyView.findViewById(R.id.cellbutton);
         if(position >= tble.getFday()-2 && position < tble.getFday()-1 + tble.getNodays()){
             b.setText(String.valueOf(position-tble.getFday()+2));
         }else{
             b.setText("0");
         }

         return MyView;
    }
}

public void updateDateInfo(){
    TextView date_info = (TextView)findViewById(R.id.date_info);
    CharSequence months[] = res.getTextArray(R.array.miesiace);
    date_info.setText(months[tble.getDay().getMM()-1].toString()+" "+tble.getDay().getYYYY());
}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Button b1 = (Button)findViewById(R.id.next_month);
            b1.performClick();
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            Button b2 = (Button)findViewById(R.id.previous_month);
            b2.performClick();
        }
        return true;
    } catch (Exception e) {
        // nothing
        Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
    }
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event){
    return myGesture.onTouchEvent(event);
}

}

and the error code:

03-02 16:30:27.301: E/AndroidRuntime(2132): FATAL EXCEPTION: main
03-02 16:30:27.301: E/AndroidRuntime(2132): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.flamis/pl.flamis.Kalendarz}: java.lang.NullPointerException
03-02 16:30:27.301: E/AndroidRuntime(2132):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.os.Looper.loop(Looper.java:123)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at java.lang.reflect.Method.invokeNative(Native Method)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at java.lang.reflect.Method.invoke(Method.java:507)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at dalvik.system.NativeStart.main(Native Method)
03-02 16:30:27.301: E/AndroidRuntime(2132): Caused by: java.lang.NullPointerException
03-02 16:30:27.301: E/AndroidRuntime(2132):     at pl.flamis.Kalendarz.onCreate(Kalendarz.java:304)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-02 16:30:27.301: E/AndroidRuntime(2132):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
03-02 16:30:27.301: E/AndroidRuntime(2132):     ... 11 more

I've spent over a day trying different solutions and nothing seems to work. I will appreciate any help. Thanks in advance!

EDIT: I attached the source code of the main class - if I change all the GestureGridView to GridView - it works like a charm, BUT fling does not work on the GridView - however it works on the rest of the layout. I need to make it work on the GridView too.

1

There are 1 answers

0
ThunderSS On BEST ANSWER

I actually found another solution, because the way I tried to do it simply didn't work and nobody knows why. I used GestureOverlayView instead, and it works great. Just had to add two swipe gestures to the gesture library.