4

我正在使用 ActionBarSherlock 并实现一个选项卡式应用程序。每个选项卡都代表一个 Fragment,它只包含一个 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。就像我之前说的,这可能与我的进口有关吗?

谢谢!!!

4

1 回答 1

15

MyWebviewFragment 扩展了扩展 Fragment 的 WebViewFragment。

您不能混合使用 API 级别 11 原生片段 ( android.app.Fragment) 和 Android 支持包片段 ( android.support.v4.app.Fragment)。您不能创建 anandroid.webkit.WebViewFragment并将其与 an 一起使用android.support.v4.app.FragmentActivity,因为android.webkit.WebViewFragmentextends android.app.Fragment,而不是android.support.v4.app.Fragment

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

于 2012-03-03T12:24:53.623 回答