第104行无法访问的代码:youTubePlayerView =(YouTubePlayerView)findViewById(R.id.youtubeplayerview);

时间:2013-11-22 20:32:02

标签: java android unreachable-code

无法访问的代码Home.java / idg / src / com / idg / omv第104行Java问题

我在第104行收到错误,指出无法访问的代码:

youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);

我不确定为什么会发生这种情况或我能做些什么来解决它(我之前从未遇到过)。从我所看到的,似乎问题是关于我的部分代码超出范围 - 我根本看不出如何解决问题。 (我尝试添加一些}但没有成功)

来源:

public class Home extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener,
        YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;
    private YouTubeThumbnailView youTubeThumbnailView3;
    private YouTubeThumbnailView youTubeThumbnailView4;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;



    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;
    Button fav_up_btn;
    Button fav_dwn_btn;

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

         // 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);

    }

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

        @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);

        youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);

        mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);

        youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
        youTubeThumbnailView1.initialize(API_KEY, this);

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

        fav_up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean favIsUp = fav_up_btn.getBackground().getConstantState()
                                            .equals(getResources().getDrawable(R.drawable.fav_up_btn).getConstantState());  
                //set the background                            
                fav_up_btn.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn : R.drawable.fav_up_btn);
            }
        });



        youTubeThumbnailView1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
        youTubeThumbnailView2.initialize(API_KEY, this);

        youTubeThumbnailView2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
        youTubeThumbnailView3.initialize(API_KEY, this);

        youTubeThumbnailView3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }
        });

        youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
        youTubeThumbnailView4.initialize(API_KEY, this);

        youTubeThumbnailView4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg1) {
                if (youTubePlayer != null) {
                    youTubePlayer.cueVideo(VIDEO1_ID);
                    youTubePlayer.play();
                    mainScrollView.smoothScrollTo(0, 0);
                }
            }

        });


    }

    @Override
    public void onInitializationFailure(Provider provider,
            YouTubeInitializationResult result) {

    }

    @Override
    public void onInitializationSuccess(Provider provider,
            YouTubePlayer player, boolean wasRestored) {

        youTubePlayer = player;

        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);
        }
    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
            YouTubeInitializationResult error) {

    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
            YouTubeThumbnailLoader thumbnailLoader) {

        youTubeThumbnailLoader = thumbnailLoader;
        thumbnailLoader
                .setOnThumbnailLoadedListener(new ThumbnailLoadedListener());

        youTubeThumbnailLoader.setVideo(VIDEO_ID);

    }

    private final class ThumbnailLoadedListener implements
            YouTubeThumbnailLoader.OnThumbnailLoadedListener {

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {

        }

    }

}

2 个答案:

答案 0 :(得分:2)

 if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);

此代码之后的所有语句都将无法访问(catch/finally除外)。这就是你看到错误的原因。

删除最后一个返回语句(或)将其置于条件块中(如第一个)。

答案 1 :(得分:1)

return super.onOptionsItemSelected(item);

此行发生在导致函数在所有情况下返回的错误之前。您需要将该语句置于某个条件(例如上面的if语句)或完全删除它。