表面已经释放(java.lang.IllegalArgumentException)

时间:2016-02-07 13:12:28

标签: android android-fragments android-mediaplayer

当按下操作栏上的操作按钮时,我正在尝试播放存储在资源下的原始文件夹中的视频文件,但是当我按下操作按钮时,应用程序崩溃,表示已经释放错误'表面已被释放错误&# 39 ;.这是我在onOptionsItemSelected()

下的媒体播放器代码
package com.example.TheWeddingApp;


import java.io.IOException;

import android.app.ActionBar;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
    final String[] data ={"Home","Venue","Gallery","Meet the Groom","Meet the Bride"};
    final String[] fragments ={
            "com.example.TheWeddingApp.FragmentOne",
            "com.example.TheWeddingApp.FragmentTwo",
            "com.example.TheWeddingApp.FragmentThree",
            "com.example.TheWeddingApp.FragmentFour",
            "com.example.TheWeddingApp.FragmentFive"};
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    SurfaceHolder surfaceHolder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mTitle = mDrawerTitle = getTitle();
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
         ActionBar bar = getActionBar();
         bar.setIcon(android.R.color.transparent);
         bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#990000")));

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

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getActionBar().setTitle(mTitle);
                    invalidateOptionsMenu();
                }

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getActionBar().setTitle(mDrawerTitle);
                    invalidateOptionsMenu();
                }
            };
            drawer.setDrawerListener(mDrawerToggle);

            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);
         final ListView navList = (ListView) findViewById(R.id.drawer);
         navList.setAdapter(adapter);
         navList.setOnItemClickListener(new OnItemClickListener(){
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                         drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                                 @Override
                                 public void onDrawerClosed(View drawerView){
                                         super.onDrawerClosed(drawerView);
                                         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                         tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                         tx.commit();
                                 }
                         });
                         drawer.closeDrawer(navList);
                 }
         });
         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
         tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
         tx.commit();
    }
       @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();

          inflater.inflate(R.menu.main_activity_bar, menu);
          return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
              return true;
            }
            switch (item.getItemId())
            {
            case R.id.action_play:
                Toast.makeText(this, "Play clicked", Toast.LENGTH_SHORT).show();
                setContentView(R.layout.video_activity);
                 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
                surfaceHolder = surfaceView.getHolder();
                surfaceHolder.setFixedSize(176, 144);
                MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setDisplay(surfaceHolder);
                  if (mediaPlayer.isPlaying()) {
                        mediaPlayer.reset();
                    }
                try {
                    mediaPlayer.setDataSource("android.resource://" + getPackageName() + "/" +"wildlife");
                    mediaPlayer.prepare();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                return true;                
            }
            return super.onOptionsItemSelected(item);
        }



}

video_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videolayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

和logcat

    02-07 18:27:44.358: E/AndroidRuntime(18440): FATAL EXCEPTION: main
02-07 18:27:44.358: E/AndroidRuntime(18440): Process: com.example.TheWeddingApp, PID: 18440
02-07 18:27:44.358: E/AndroidRuntime(18440): java.lang.IllegalArgumentException: The surface has been released
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.media.MediaPlayer._setVideoSurface(Native Method)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.media.MediaPlayer.setDisplay(MediaPlayer.java:688)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.example.TheWeddingApp.MainActivity.onOptionsItemSelected(MainActivity.java:134)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.app.Activity.onMenuItemSelected(Activity.java:2608)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.view.View.performClick(View.java:4456)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.view.View$PerformClick.run(View.java:18465)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.os.Handler.handleCallback(Handler.java:733)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.os.Looper.loop(Looper.java:136)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at android.app.ActivityThread.main(ActivityThread.java:5086)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at java.lang.reflect.Method.invoke(Method.java:515)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-07 18:27:44.358: E/AndroidRuntime(18440):    at dalvik.system.NativeStart.main(Native Method)

似乎无法弄清楚问题,感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您的活动还应实施:implements SurfaceHolder.Callback并将mediaPlayer.setDisplay(surfaceHolder);置于覆盖public void surfaceCreated(SurfaceHolder holder)方法中。

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer.setDisplay(holder);
}