سؤال

I am using ActionBarSherlock and implementing a tabbed app. Each tab represents a Fragment that contains nothing but a WebView. I've got it implemented with my object derived from Fragment. But when I changed it to WebViewFragment, I can no longer add it to the FragmentTransaction. I'm wondering if I importing the right things? Here's the code:

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;
    }

The problem lines are:

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

 ....

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

I can't figure out why. MyWebviewFragment extends WebViewFragment which extend Fragment. FragmentTransaction methods should see MyWebviewFragment as if it were a simple Fragment. Like I said earlier, could this have something to do with my imports?

Thanks!!!

هل كانت مفيدة؟

المحلول

MyWebviewFragment extends WebViewFragment which extend Fragment.

You cannot mix API Level 11 native fragments (android.app.Fragment) and Android Support package fragments (android.support.v4.app.Fragment). You cannot create an android.webkit.WebViewFragment and use it with an android.support.v4.app.FragmentActivity, because android.webkit.WebViewFragment extends android.app.Fragment, not android.support.v4.app.Fragment.

Either do not use ActionBarSherlock and the Android Support package, by creating an API Level 11+ app, or do not use WebViewFragment (or copy it from the source code and refactor it into your project).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top