2014-04-14 78 views
0

我開發的android youtube api.i成功創建了youtube api項目,並且我可以在活動開始時播放youtube視頻。現在我想播放視頻只在按鈕click.i有nullpointexception 這是我的代碼Android Youtube Api nullPointException

public class MainActivity extends YouTubeBaseActivity implements 
    YouTubePlayer.OnInitializedListener { 

static private final String DEVELOPER_KEY = "************"; 
static private final String VIDEO = "hjHnWz3EyHs"; 
private Button b1; 
private YouTubePlayer player; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view); 
    b1 = (Button) findViewById(R.id.button1); 
    youTubeView.initialize(DEVELOPER_KEY, this); 

    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      player.loadVideo(VIDEO, 36000); //error is in this line 


     } 
    }); 
} 

@Override 
public void onInitializationFailure(Provider provider, 
     YouTubeInitializationResult error) { 
    Toast.makeText(this, "Oh no! " + error.toString(), Toast.LENGTH_LONG) 
      .show(); 
} 

@Override 
public void onInitializationSuccess(Provider provider, 
     YouTubePlayer player, boolean wasRestored) { 
    //player.loadVideo(VIDEO, 36000); 
} 

} 什麼是錯的?如果有人知道解決方案,請幫助我 謝謝

+0

刪除按鈕單擊該行並刪除'onInitializati中的註釋onSuccess'它會工作。這個實例可放大的播放器永遠不會初始化 – Raghunandan

回答

0

你缺少player初始化在onInitializationSuccess方法,它應該是如下:

@Override 
public void onInitializationSuccess(Provider provider, 
    YouTubePlayer player, boolean wasRestored) { 
    MainActivity.this.player = player; 
    //do other stuff like customizing player 
} 

也請看一看樣品文件夾here

+0

我添加了這個(MainActivity.this.player = player;)但是當我點擊按鈕時我有nullpointException – user3345767

+0

@ user3345767你確定你沒有任何初始化失敗嗎? – nikis

+0

我可以在應用程序啓動時播放視頻,但是我想僅在按鈕點擊時開始播放視頻 – user3345767