无法将WebViewFragment派生类添加到FragmentTransaction

时间:2012-03-03 08:45:14

标签: android actionbarsherlock

我正在使用ActionBarSherlock并实现一个标签式应用。每个选项卡代表一个只包含WebView的片段。我已经用从Fragment派生的对象实现了它。但是当我将其更改为WebViewFragment时,我无法再将其添加到FragmentTransaction中。我想知道我是否输入正确的东西?这是代码:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewFragment;
import android.widget.TextView;

public class WebTab1Fragment extends FragmentActivity {
    int mStackLevel = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webtab1_layout);


    if (savedInstanceState == null) {
        // Do first time initialization -- add initial fragment.
        MyWebvewFragment newFragment = MyWebviewFragment.newInstance(mStackLevel);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.simple_fragment, newFragment).commit();

    } else {
        mStackLevel = savedInstanceState.getInt("level");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("level", mStackLevel);
}


void addFragmentToStack() {
    mStackLevel++;

    // Instantiate a new fragment.
    MyWebviewFragment newFragment = MyWebviewFragment.newInstance(mStackLevel);

    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.simple_fragment, newFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}



public static class MyWebviewFragment extends WebViewFragment {
    int mNum;
    private WebView webview = null;
    private ProgressDialog mSpinner = null;
    private static final int DIALOG_PROGRESS = 1;
    private Handler mProgressHandler;
    boolean bFinishFlag = true;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
       mProgressHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (bFinishFlag == true)
                    mSpinner.dismiss();
                else
                    mProgressHandler.sendEmptyMessageDelayed(0, 100);
            }
        };

        super.onActivityCreated(savedInstanceState);
    }

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static MyWebviewFragment newInstance(int num) {
        MyWebviewFragment f = new MyWebviewFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

问题在于:

ft.add(R.id.simple_fragment, newFragment).commit();

 ....

 ft.replace(R.id.simple_fragment, newFragment);

我无法弄清楚原因。 MyWebviewFragment扩展了扩展Fragment的WebViewFragment。 FragmentTransaction方法应该将MyWebviewFragment看作是一个简单的Fragment。就像我之前说的那样,这可能与我的进口有关吗?

感谢!!!

1 个答案:

答案 0 :(得分:15)

  

MyWebviewFragment扩展了扩展Fragment的WebViewFragment。

您不能混用API Level 11原生片段(android.app.Fragment)和Android支持包片段(android.support.v4.app.Fragment)。您无法创建android.webkit.WebViewFragment并将其与android.support.v4.app.FragmentActivity一起使用,因为android.webkit.WebViewFragment扩展了android.app.Fragment,而不是android.support.v4.app.Fragment

要么不使用ActionBarSherlock和Android支持包,要么创建API Level 11+应用程序,要么不使用WebViewFragment(或从源代码复制它并将其重构到您的项目中)。