android - Youtube 播放器因在 Android 中提供 java.lang.IllegalStateException : android. os.DeadObjectException 而崩溃

标签 android android-youtube-api

我正在做一个项目。我在 Activity 中使用 YouTubeAndroidPlayerAPIWebview。 当我尝试使用其 ID 运行视频时,它运行了。问题是,但如果视频 ID 为空,那么我不会按照代码中的说明运行或初始化视频,然后它会给我以下 logcat 输出,我的应用程序会崩溃,并给出消息 "Unfortunately You tube已经停止”。任何人都可以帮助我,因为我做了很多搜索但找不到任何解决方案。 我的 Activity 扩展 SherlockYouTubeActivitySherlockYouTubeActivity 包含 SherlockActivity 代码,但尽管有 Activty,它还是扩展了 YouTubeBaseActivity。因此,通过这样做,我通过 Multilevel InheritanceSherlockActivityYouTubeBaseActivity 扩展到了我的 Activity。

我的 LogCat 输出 如下:-

09-09 18:41:18.201: E/AndroidRuntime(25534): FATAL EXCEPTION: main
09-09 18:41:18.201: E/AndroidRuntime(25534): java.lang.IllegalStateException: android.os.DeadObjectException
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.K(SourceFile:229)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.apps.youtube.api.jar.a.a.a(SourceFile:62)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.apps.youtube.api.jar.a.s.b(SourceFile:945)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.apps.youtube.api.jar.y.a(SourceFile:180)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.apps.youtube.api.jar.a.a.k(SourceFile:576)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.e(SourceFile:51)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.youtube.api.jar.client.c.run(SourceFile:715)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at android.os.Handler.handleCallback(Handler.java:730)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at android.os.Looper.loop(Looper.java:176)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at android.app.ActivityThread.main(ActivityThread.java:5419)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at java.lang.reflect.Method.invoke(Method.java:525)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at dalvik.system.NativeStart.main(Native Method)
09-09 18:41:18.201: E/AndroidRuntime(25534): Caused by: android.os.DeadObjectException
09-09 18:41:18.201: E/AndroidRuntime(25534):    at android.os.BinderProxy.transact(Native Method)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.apps.youtube.api.b.a.aq.e(SourceFile:466)
09-09 18:41:18.201: E/AndroidRuntime(25534):    at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.K(SourceFile:226)
09-09 18:41:18.201: E/AndroidRuntime(25534):    ... 15 more

我的 XML 文件是:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.ads.doubleclick.DfpAdView 
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        ads:adUnitId="MYADDID"
        ads:adSize="BANNER"
        />
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/ScrollView_StoryDetails2"
        android:layout_above="@+id/adView">

    <RelativeLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

    <WebView android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        />

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        />
    </RelativeLayout>
    </ScrollView>



        <TextView 
            android:id="@+id/tNoNet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="No Internet Connection, Reading Offline."
            android:layout_centerHorizontal="true"
            android:textColor="#000000"
            android:layout_gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="12sp"
            android:typeface="serif"
            android:background="#FF7E00"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"/>

</RelativeLayout>

我的 Activity 类如下:-

public class StoryDetail extends SherlockYouTubeActivity implements
YouTubePlayer.OnInitializedListener 
{

    private String sVideoId;
    public String html;
    String sCategory;

    SharedPreferences sPref;

    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "MY_YOUTUBE_DEVELOPER_KEY";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    public boolean netConnected=false;

    WebView content;
    WebView myWebView;
    WebView wvAdd;
    TextView tNoNet;
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.story_detail2);

        Bundle b=new Bundle();
        b=getIntent().getExtras();

     // Get the vedioId...
            sVideoId = b.getString("videoId");

                YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        if(sVideoId!=null)
        {
            try{
             youTubeView.initialize(YoutubeDeveloperKey, this);
            }
            catch(IllegalStateException e){
                e.printStackTrace();
            }
        }
        else{
            youTubeView.setVisibility(8);//Gone
        }


        ActionBar bar = getSupportActionBar();
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayUseLogoEnabled(false);
        bar.setDisplayHomeAsUpEnabled(true);

        tNoNet=(TextView)findViewById(R.id.tNoNet);

        checkInternetConnection();

        // Create an image loader
        //mImageLoader = new ImageLoader(3);

        // Look up the DfpAdView as a resource and load a request.
        DfpAdView adView = (DfpAdView)this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());

        // Get the fields
        content = (WebView) findViewById(R.id.content);
        content.getSettings().setPluginState(PluginState.ON_DEMAND);
        content.getSettings().setJavaScriptEnabled(true);
        content.getSettings().setAllowFileAccess(true);
        content.setWebChromeClient(new WebChromeClient());
        content.setWebViewClient(new MyAppWebViewClient());

    html = "<html>SOME_STUFFS TO SHOW ON WEBVIEW</html>";

    content.loadDataWithBaseURL("http://bgr.in/", html, "text/html", "UTF-8", "");

    }

    public checkInternetConnection(){
    // Some Code Goes Here...
    }


     @Override
    public void onBackPressed()
    {
        if(content.canGoBack()){
            content.goBack();

        }
        else
            super.onBackPressed();
    }
    /**
     * Menu handling
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.story_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.share:
                /**
                *Some Code Goes Here                
                */
                return true;
            case R.id.comments:
                /**
                *Some Code Goes Here                
                */
                return true;
            case android.R.id.home:
                // This is called when the Home (Up) button is pressed
                // in the Action Bar.
                /**
                *Some Code Goes Here                
                */
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPause() {
        super.onPause();
       content.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        content.onResume();

        }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        content.loadUrl("about:blank");

    }

    @Override
    public void onInitializationFailure(Provider arg0,
            YouTubeInitializationResult arg1) {
        // TODO Auto-generated method stub
        if (arg1.isUserRecoverableError()) {
            arg1.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(
                    "There was an error initializing the YouTubePlayer",
                    arg1.toString());
        }
    }


    @Override
    public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
            boolean arg2) {
        // TODO Auto-generated method stub
        if (!arg2) {
            YPlayer = arg1;
            if(sVideoId!=null){
                try{
                 YPlayer.loadVideo(sVideoId);
                }
                catch(IllegalStateException e){
                    e.printStackTrace();
                }
            }
            }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOVERY_DIALOG_REQUEST) {
        // Retry initialization if user performed a recovery action
        if(sVideoId!=null){
            getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
        }

    }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }

}

任何帮助都会发自内心地感激。提前致谢..

最佳答案

上面的代码现在可以正常工作了,造成该异常的所有原因是,一些 VideoId 为空,一些为“”,因此值为“”的 VideoId 对我造成了异常,因为我只是在检查对于空值而不是 VideoId 的“”值。现在问题已经解决了。

如果任何人需要使用 Youtube Android API 代码可以简单地使用上面的代码。即使在 Sherlock Activity 中也能正常工作。

感谢那些尝试和支持我的人的反馈。

关于android - Youtube 播放器因在 Android 中提供 java.lang.IllegalStateException : android. os.DeadObjectException 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25747226/

相关文章:

android - 为android中的应用程序制作小部件

android - 如何强制Android使用wifi网络而不是移动网络

android - 在堆栈 flutter 中使页面下方可点击

android - 在 Android 应用程序中嵌入 YouTube 直播

android - Android和YoutubePlayer:创建YouTubePlayerView时出错

android - YouTubeAndroidPlayerAPI 无法播放某些视频

android - Android 中 ScrollView 内的 Listview 高度问题

java - 不能 Intent 进行另一项 Activity

android - 在 YoutubePlayerFragment 中显示受限制的 youtube 视频?

android - 无法实例化 com.google.android.youtube.player.YouTubePlayerView