java.lang.IllegalStateException:Activity已被销毁异常

时间:2013-12-09 14:58:06

标签: android android-fragments android-viewpager

我正在实施一个播放视频的应用程序。共有2项活动:MainMenuActivity(纵向模式)和VideoPlayerActivity(可以旋转)。 MainMenuActivity有HomeFragmentLT个片段。在HomeFragmentLT片段中有一个ViewPager。这是我的HomeFragmentLT和ViewPager(HomePager):

public class HomeFragmentLT extends Fragment implements Observer
{
    public static String TAG = "HomeFragmentLT";
    private ViewGroup mLayout;
    private View mHeader;
    private HomeAdapter mHomeAdapter;       
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.e(TAG, "onCreate");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (mLayout == null)        
        mLayout = (ViewGroup) inflater.inflate(R.layout.home_listview, null);       
    else        
        ((ViewGroup) mLayout.getParent()).removeView(mLayout);      
    initViews();        
    return mLayout;
}

public HomeFragmentLT(){}

private void initViews()
{       
    mPager = (ViewPager) mHeader.findViewById(R.id.home_pager);
    .......
    .......
}

@Override
public void onStart()
{
    super.onStart();
    if (!mDataReceived)
        getData();      
    Log.e(TAG, "onStart");
}

@Override
public void onStop()
{
    DataHelper.getInstance().deleteObserver(this);
    super.onStop();
    Log.e(TAG, "onStop");
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.e(TAG, "onDestroy");
}

private void getData()
{
    if (mError)
    {
        UIUtils.toggleError(mLayout, false, null);
        mListView.setOnTouchListener(null);
        UIUtils.toggleLoading(mLayout, true);
    }
    DataHelper.getInstance().addObserver(this);
    DataHelper.getInstance().getHomeData();
}

private void setData()
{
    mHomeAdapter = new HomeAdapter(getActivity(), mPager, mHomeData.getHotPrograms());
    mPager.setAdapter(mHomeAdapter);
    mIndicator.setViewPager(mPager);

    .............
}

@Override
public void update(Observable observable, Object data)
{
    if (data == null)
        return;
    Result result = (Result) data;
    if (Method.GET_HOME_DATA.equals(result.getParams().getMethod()))
    {
        if (result.getError() == null)
        {
            mHomeData = (HomeObj) result.getData();
            setData();
            mDataReceived = true;
            mError = false;
            DataHelper.getInstance().deleteObserver(this);
        }           
    }
}
......................
public static class HomeAdapter extends PagerAdapter
{
    private List<Program> mPrograms;
    private View[] mViews;
    private Context mContext;

    public HomeAdapter(Context context, ViewPager viewpager, List<Program> programs){...........}

    @Override
    public int getCount()
    {
        return mPrograms.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        View view;
        if ((view = mViews[position]) != null)
        {
            ImageView img = (ImageView) view.findViewById(R.id.img);
            if (img.getDrawable() == null)
            {
                UIUtils.toggleLoading(view, true);
                ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(), img, mLoadingListener);
            }
            return view;
        }

        view = LayoutInflater.from(mContext).inflate(R.layout.home_program_item_top, null);
        view.setTag(mPrograms.get(position).getId());
        view.setLayoutParams(mParams);
        if (Constants.MAY_CUI_DETECTED)
            ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(),
                    (ImageView) view.findViewById(R.id.img), mParams.width, mParams.height, mLoadingListener);
        else
            ImageManager.getInstance().push(mPrograms.get(position).getThumbnail(),
                    (ImageView) view.findViewById(R.id.img), mLoadingListener);

        view.setOnClickListener(mOnItemClick);
        container.addView(view);
        return mViews[position] = view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object){}
}   
}

MainMenuActivity中,我在HomeFragmentLT

中添加了onCreate()
getSupportFragmentManager().beginTransaction().add(R.id.dummy, mHomeFragment).commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();

一切正常。但是当我将手机置于风景中时,请启动VideoPlayerActivity(在横向模式下表示VideoPlayerActivity)从MainMenuActivity(肖像)播放视频并立即按回按钮。它有例外。这是我的logcat:

12-09 18:30:09.102: E/MainMenuActivity(23287): onCreate()
12-09 18:30:09.332: E/MainMenuActivity(23287): onStart()
12-09 18:30:09.432: E/MainMenuActivity(23287): onResume()
12-09 18:30:09.703: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:09.703: E/HomeFragmentLT(23287): onCreateView()
...........
12-09 18:30:14.487: E/MainMenuActivity(23287): onPause()
12-09 18:30:14.788: E/VideoPlayerActivity(23287): onCreate()
12-09 18:30:14.788: E/VideoPlayerActivity(23287): onStart()
...........
12-09 18:30:15.418: E/VideoPlayerActivity(23287): onPause()
12-09 18:30:15.588: E/HomeFragmentLT(23287): onStop()
12-09 18:30:15.639: E/MainMenuActivity(23287): onDestroy()
12-09 18:30:15.659: E/MainMenuActivity(23287): onCreate()
12-09 18:30:15.659: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:15.719: E/MainMenuActivity(23287): onStart()
12-09 18:30:15.789: E/HomeFragmentLT(23287): onCreateView()
12-09 18:30:15.979: E/MainMenuActivity(23287): onResume()
12-09 18:30:16.169: E/MainMenuActivity(23287): onPause()
12-09 18:30:16.169: E/HomeFragmentLT(23287): onStop()
12-09 18:30:16.179: E/MainMenuActivity(23287): onDestroy()
12-09 18:30:16.199: E/MainMenuActivity(23287): onCreate()
12-09 18:30:16.199: E/HomeFragmentLT(23287): onCreate()
12-09 18:30:16.239: E/MainMenuActivity(23287): onStart()
12-09 18:30:16.259: E/HomeFragmentLT(23287): onCreateView()
12-09 18:30:16.379: E/MainMenuActivity(23287): onResume()
12-09 18:30:16.469: E/AndroidRuntime(23287): FATAL EXCEPTION: main
12-09 18:30:16.469: E/AndroidRuntime(23287): java.lang.IllegalStateException: Activity has been destroyed
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at com.vng.zingtv.activity.MainMenuActivity$3.run(MainMenuActivity.java:375)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.os.Handler.handleCallback(Handler.java:615)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.os.Looper.loop(Looper.java:137)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at android.app.ActivityThread.main(ActivityThread.java:4744)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at java.lang.reflect.Method.invoke(Method.java:511)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 18:30:16.469: E/AndroidRuntime(23287):    at dalvik.system.NativeStart.main(Native Method)

我需要你的帮助有两个问题 1.为什么MainMenuActivity被销毁并重新创建? (我的LogCat)
2.导致HomeFragmentLT的{​​{1}}片段出了什么问题?

提前致谢。

修改
我希望我的VideoPlayerActivity根据设备方向旋转。所以在manifiest.xml中

IllegalStateException

和VideoPlayerActivity的onCreate():

<activity
     android:name=".activity.VideoPlayerActivity"
     android:screenOrientation="portrait" />

2 个答案:

答案 0 :(得分:1)

旋转设备意味着系统必须加载新资源。例如。您可能在某些layout-land文件夹中有不同的资源,需要加载。为了能够加载这些新资源,活动将被销毁,并使用新的drawables / layouts / values等重新创建.Android有一个特殊的机制来进行此类更改,称为configChanges。这包括设备轮换,还包括更改手机语言等等。

为避免您的活动被销毁,您可以在AndroidManifest.xml中声明,您将通过设置此标记来自行处理方向更改:

<activity
    android:name=".VideoPlayerActivity"
    android:configChanges="orientation|screenSize" />

并在您的活动中添加以下代码:

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

有关详细信息,请参阅this link to the developer documentation

答案 1 :(得分:0)

我不知道为什么它导致MainMenuActivity被销毁并重新创建2次因为MainMenuActivity被旋转。(我没有看到MainMenuActivity是旋转的,只是从logcat猜测)。为避免这种情况,我在manifiest.xml中添加了android:configChanges="orientation|keyboardHidden|screenSize"

<activity
      android:name=".activity.MainMenuActivity"
      android:screenOrientation="portrait"
      android:configChanges="orientation|keyboardHidden|screenSize"/>

现在,我可以在MainMenuActivity中看到onConfigurationChanged()中的日志,并避免销毁和重新创建MainMenuActivity。 但我仍然不知道为什么它会被摧毁。 现在是log cat:

12-10 00:00:16.865: E/VideoPlayerActivity(20105): onPause()
12-10 00:00:16.885: E/MainMenuActivity(20105): onConfigurationChanged LANDSCAPE
12-10 00:00:16.885: E/MainMenuActivity(20105): onResume()
12-10 00:00:17.296: E/MainMenuActivity(20105): onConfigurationChanged PORTRAIT
12-10 00:00:17.686: E/VideoPlayerActivity(20105): onStop()
12-10 00:00:17.686: E/VideoPlayerActivity(20105): onDestroy()