0

我开始学习android并且遇到了问题。在“OnFling”方法中的手势监听器上,我有一个带有 e1 参数的 NullPointerException,但 e2 arg 指向正常。请帮忙。

我的活动代码如下:

public class MainActivity extends Activity {

    public dbWorker d = null;
    public ArrayList<Row> newsContainer = new ArrayList<Row>();
    private static ArrayList<Row> cached = new ArrayList<Row>();
    public static int cache_guid = 0;
    public static Row selectedItem = null;


    private ListView newsList = null;
    ListArrayAdapter aa = null;

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;

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

        d = new dbWorker(this);
        cache_guid = d.getMaxGuid();
        newsList = (ListView) findViewById(R.id.listView1);

        aa = new ListArrayAdapter(this, newsContainer);
        newsList.setAdapter(aa);
        newsList.setClickable(true);
        newsList.setBackgroundColor(color.holo_blue_dark);

        refreshRssList();
        setTitle("Rss Reader/ Main page");
        gestureDetector = new GestureDetector(this,new MyGestureDetector(), null, false);

        // Set the touch listener for the main view to be our custom gesture listener
        newsList.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

        newsList.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

            }

        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Intent intent = new Intent(MainActivity.this.getBaseContext(), FavActivity.class);

            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                return false;
            }

            /* right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            startActivity(intent);
            MainActivity.this.overridePendingTransition(
            R.anim.slide_in_right,
            R.anim.slide_out_left
            );

            // right to left swipe
            }  else */
            if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            startActivity(intent);
            MainActivity.this.overridePendingTransition(
            R.anim.slide_in_left, 
            R.anim.slide_out_right
            );
            }

            return false;
        }

        // It is necessary to return true from onDown for the onFling event to register
        @Override
        public boolean onDown(MotionEvent e) {
                return true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    private void refreshRssList() {

        ArrayList<Row> newItems = rssWorker.getNews(this);
        ArrayList<Row> for_save = new ArrayList<Row>();

        if (dbWorker.readAll() != null)
            cached = dbWorker.readAll();

        if (newItems == null)
        {
            Toast.makeText(this, "Connection error!", 3).show();
            return;
        }


        for (int i = 0; i < newItems.size(); i++)
        {
            Row tmp = newItems.get(i);
            cached.add(tmp);
            for_save.add(tmp);
        }

        newsContainer.clear();

        if (cached != null)
            newsContainer.addAll(cached);

        d.SaveAll(for_save);

        aa.notifyDataSetChanged();
      }
}

和 ArrayAdapter:

包 com.example.rssreader;

public final class ListArrayAdapter extends ArrayAdapter<Row> {

    private final Context c;
    private final List<Row> items;

    public ListArrayAdapter(Context context, List<Row> objects) {
        super(context, R.layout.list, objects);
        c = context;
        items = objects;
    }


      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {

          final Row selectedItem = items.get(position);

          LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rowView = inflater.inflate(R.layout.list, parent, false);
          TextView newsTitle = (TextView) rowView.findViewById(R.id.titleView);
          final ImageView favIcon = (ImageView) rowView.findViewById(R.id.imageView1);
          LinearLayout newsLay = (LinearLayout) rowView.findViewById(R.id.listLayout);

          Date d = new Date();
          d.setTime(selectedItem.pubdate);
          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");

          newsTitle.setText(selectedItem.name + " " + sdf.format(d));

          if (selectedItem.isRead == 0)
              newsLay.setBackgroundColor(Color.BLUE);
          else
              newsLay.setBackgroundColor(Color.GRAY);

          if (selectedItem.isFav ==  1)
          {
                favIcon.setImageResource(R.drawable.ic_tab_name_selected);
            } else {
                favIcon.setImageResource(R.drawable.ic_tab_name_unselected);
            }
          favIcon.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                if (selectedItem.isFav == 1){
                    favIcon.setImageResource(R.drawable.ic_tab_name_unselected);
                    selectedItem.isFav = 0;
                }
                else
                {
                    favIcon.setImageResource(R.drawable.ic_tab_name_selected);
                    selectedItem.isFav = 1;
                }
                dbWorker.SaveState(selectedItem);
            }

          });
          newsTitle.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    MainActivity.selectedItem = selectedItem;
                    c.startActivity(new Intent(v.getContext(), ItemActivity.class));

                    if (selectedItem.isRead != 1)
                    {
                        selectedItem.isRead = 1;
                        dbWorker.SaveState(selectedItem);
                    }
                }
          });
          return rowView;
        }

}
4

1 回答 1

0

在尝试将 onFling 侦听器包含在 ListViews 中时,我曾经遇到过这种情况。似乎如果您的 ListView 仍在检测某些触摸事件(例如,触摸或正常滚动),它将消耗运动事件。因此,您必须为 MotionEvents 有时为空做好准备。

于 2013-08-14T19:29:56.313 回答