Android IllegalStateException:片段未附加到活动webview

时间:2014-03-13 00:14:09

标签: java android android-fragments android-webview illegalstateexception

所以我是Android应用程序写作的新手,我正在努力开发一个练习应用程序,我希望以后可以转化为某些东西。在我决定尝试将webview添加到其中一个之前,我在操作栏中有3个选项卡运行正常。现在它崩溃了IllegalStateException。由于我现在对机器人知之甚少,我似乎无法弄清楚出了什么问题。

主要活动:

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Web", "Facebook", "Twitter" };

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) 
    {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() 
    {

        @Override
        public void onPageSelected(int position) 
        {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) 
        {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) 
        {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) 
{
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) 
{
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) 
{
}

使用webview的WebFragment:

public class WebFragment extends Fragment 
{

private String url = getString(R.string.website);

//@Override
//public void onActivityCreated(Bundle savedInstanceState) 
//{
    //super.onActivityCreated(savedInstanceState);
//}

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{

    View rootView = inflater.inflate(R.layout.web_fragment, container, false);

    WebView tolerableWebView = (WebView) getView().findViewById(R.id.webview);
    tolerableWebView.getSettings().setJavaScriptEnabled(true);
    tolerableWebView.loadUrl(url);

    return rootView;
}

}

TabsPagerAdapter:

public class TabsPagerAdapter extends FragmentPagerAdapter
{

public TabsPagerAdapter(FragmentManager fm) 
{
    super(fm);
}

@Override
public Fragment getItem(int index) 
{

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new WebFragment();
    case 1:
        // Games fragment activity
        return new FacebookFragment();
    case 2:
        // Movies fragment activity
        return new TwitterFragment();
    }

    return null;
}

@Override
public int getCount() 
{
    // get item count - equal to number of tabs
    return 3;
}
}

webfragment xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

</WebView>

主要活动xml:

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

任何帮助都会很棒!感谢

1 个答案:

答案 0 :(得分:87)

通常,在Fragment不再附加到Activity后尝试执行工作时会出现该错误。在触发IllegalStateException的回调中,添加对isAdded的检查:http://developer.android.com/reference/android/app/Fragment.html#isAdded()

if(!isAdded()) {
    return;
}