本文介绍了安卓IllegalStateException异常:片段未连接到活动的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是新来的Andr​​oid应用程序写作,我努力工作,在实践中的应用程序,我希望变成的东西后。我在细跑之前,我决定尝试添加的WebView其中之一的动作条有3个标签。现在它崩溃与一个IllegalStateException。而且因为我不知道太多关于Android的那一刻,我似乎无法弄清楚什么是错的。

So I'm new to android app writing, and I am trying to work on a practice app that I can hopefully turn into something later. I had 3 tabs in the actionbar that ran fine before I decided to try to add webview to one of them. Now it crashes with an IllegalStateException. And since I don't know too much about android at the moment, I can't seem to figure out what is wrong.

主要活动:

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)
{
}

通过web视图的WebFragment:

The WebFragment with the webview:

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:

the 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

The 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:

The main activity 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"/>

任何帮助将是巨大的!谢谢

Any help would be great! Thanks

推荐答案

通常当您尝试后,片段将不再附加到活动执行工作的得到这个错误。在触发IllegalStateException异常增加一个检查isAdded回调:http://developer.android.com/reference/android/app/Fragment.html#isAdded()

Generally you get that error when you try to perform work after the Fragment is no longer attached to the Activity. In the callback that triggers the IllegalStateException add a check for isAdded: http://developer.android.com/reference/android/app/Fragment.html#isAdded()

if(!isAdded()) {
    return;
}

这篇关于安卓IllegalStateException异常:片段未连接到活动的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:27