【问题标题】:Cannot Reach Breakpoint / Intent Will Not Launch无法到达断点/Intent 不会启动
【发布时间】:2013-12-10 12:59:08
【问题描述】:

我正在尝试在我的 Home.java 中启动一个意图:

@Override
public void onVideoClicked(Video video) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(video.getUrl()));
    startActivity(intent);
}

它应该由以下人发起:

package com.idg.omv.ui;

import com.idg.omv.domain.Video;

public interface VideoClickListener {

    public void onVideoClicked(Video video);

}

这是由我的 Home.java 中的listView.setOnVideoClickListener(this); 发起的

但是,在 Home.java 中设置断点时,我似乎无法到达 onVideoClicked,我不确定为什么。

完整来源:

public class Home extends YouTubeBaseActivity implements

VideoClickListener {
    // A reference to our list that will hold the video details
    private VideosListView listView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    private int mCurrentTabPosition = NO_CURRENT_POSITION;
    private static final int NO_CURRENT_POSITION = -1;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;
    private ViewPager mPager;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    String TAG = "DEBUG THIS";
    String PLAYLIST = "idconex";
    Activity activity;
    int imageArray[];
    String[] stringArray;

    private OnPageChangeListener mPageChangeListener;
    ImagePagerAdapter adapter = new ImagePagerAdapter();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        final ActionBar actionBar = getActionBar();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        viewPager.setAdapter(adapter);

        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);

        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        drawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description */
        R.string.drawer_close /* "close drawer" description */
        );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        //mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
        listView = (VideosListView) findViewById(R.id.videosListView);
        // Here we are adding this activity as a listener for when any row in
        // the List is 'clicked'
        // The activity will be sent back the video that has been pressed to do
        // whatever it wants with
        // in this case we will retrieve the URL of the video and fire off an
        // intent to view it
        listView.setOnVideoClickListener(this);
        new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();

    }




    Handler responseHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    // This is the interface method that is called when a video in the listview
    // is clicked!
    // The interface is a contract between this activity and the listview

    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(video.getUrl()));
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
        // true
        // then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class ImagePagerAdapter extends PagerAdapter {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        // this is your constructor
        public ImagePagerAdapter() {
            super();
            // setOnPageChangeListener(mPageChangeListener);
        }

        private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn, R.drawable.country_up_btn,
                R.drawable.dance_up_btn, R.drawable.hiphop_up_btn };

        private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
                "EminemVEVO‎" };

        @Override
        public int getCount() {
            return mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);

            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }

        private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(final int position) {
                onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
                mCurrentTabPosition = position;

            }
        };

        protected void onTabChanged(final PagerAdapter adapter,
                final int oldPosition, final int newPosition) {
            // Calc if swipe was left to right, or right to left
            if (oldPosition > newPosition) {
                // left to right
            } else {
                // right to left

                View vg = findViewById(R.layout.home);
                vg.invalidate();
            }
            final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

            viewPager.setOnPageChangeListener(new OnPageChangeListener() {

                int oldPos = viewPager.getCurrentItem();

                @Override
                public void onPageScrolled(int position, float arg1, int arg2) {

                    if (position > oldPos) {
                        // Moving to the right

                    } else if (position < oldPos) {
                        // Moving to the Left

                        View vg = findViewById(R.layout.home);
                        vg.invalidate();
                    }
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub

                }

            });

        }
    }
}

VideoListView.java

public class VideosListView extends ListView implements android.widget.AdapterView.OnItemClickListener {

    private List<Video> videos;
    private VideoClickListener videoClickListener;

    public VideosListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VideosListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VideosListView(Context context) {
        super(context);
    }

    public void setVideos(List<Video> videos){
        this.videos = videos;
        VideosAdapter adapter = new VideosAdapter(getContext(), videos);
        setAdapter(adapter);
        // When the videos are set we also set an item click listener to the list
        // this will callback to our custom list whenever an item it pressed
        // it will tell us what position in the list is pressed
        setOnItemClickListener(this);
    }

    // Calling this method sets a listener to the list
    // Whatever class is passed in will be notified when the list is pressed
    // (The class that is passed in just has to 'implement VideoClickListener'
    // meaning is has the methods available we want to call)
    public void setOnVideoClickListener(VideoClickListener l) {
        videoClickListener = l;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
    }

    // When we receive a notification that a list item was pressed
    // we check to see if a video listener has been set
    // if it has we can then tell the listener 'hey a video has just been clicked' also passing the video
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
        if(videoClickListener != null){
            videoClickListener.onVideoClicked(videos.get(position));
        }
    }
}

我的来源: https://www.dropbox.com/s/irab3x18nhj4twt/idg.zip

工作示例: http://blog.blundell-apps.com/click-item-in-a-listview-to-show-youtube-video/

【问题讨论】:

  • 你在哪里设置和调用那个接口方法,我哪里都看不到
  • 什么是 VideosListView?
  • 检查VideoListView 中的onItemClick 监听器是否被调用。
  • 我刚才在那里设置了一个断点 - 没有到达
  • OnItemClickListener 仅在 ListView 上设置视频时设置。仅当 GetYouTubeUserVideosTask 成功完成时,才会在 ListView 上设置视频。可以?您的清单中有互联网权限吗?

标签: java android android-intent


【解决方案1】:

如果列表有ImageViewButton等可点击元素,当您点击列表时,ImageViewButton会使用它,而您的onitemClickListener永远不会被调用。

要使其正常工作,请添加 android:clickable="false"android:focusable="false"android:focusableInTouchMode="false" 到自定义适配器视图中的每个 ButtonsImageViews

list_item_user_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.idg.omv.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
        android:contentDescription="YouTube video thumbnail"
        android:gravity="center"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" 
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
   <View android:layout_width="match_parent" 
      android:layout_height="2dp"
      android:visibility="invisible"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/userVideoTitleTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft = "5dip"
            android:textSize="16sp"
            android:text="Video Title Not Found"
            android:textColor="@android:color/black" />

           <TextView
            android:id="@+id/userVideouploaderTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:paddingLeft = "5dip"
            android:layout_below="@id/userVideoTitleTextView"
            android:textColor="@android:color/black" />



        <Button
            android:id="@+id/fav_up_btn1"
            android:layout_width="27dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/fav_up_btn1"
            android:gravity="right"
            android:paddingRight="5dp"
            android:paddingTop="5dp" 
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

    </RelativeLayout>
   <View android:layout_width="match_parent" 
      android:layout_height="11dp"
      android:visibility="invisible"/>
</LinearLayout>

查看这篇文章了解更多信息 List view doesn't respond to click

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多