我正在使用ActionBarSherlock並實現選項卡式應用程序。每個選項卡表示一個只包含WebView的片段。我已經用從Fragment派生的對象實現了它。但是當我將其更改爲WebViewFragment時,我不能再將其添加到FragmentTransaction中。我想知道我是否輸入了正確的東西?下面的代碼:無法將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。就像我之前說的,這可能與我的進口有關嗎?
謝謝!
我很害怕這個。我希望他們很快將WebViewFragment添加到Android支持包中。對於那些希望在所有平臺上運行而仍然使用碎片的人來說,這是一個很大的漏洞。 – JustLearningAgain 2012-03-03 19:39:38
@JustLearningAgain:'WebViewFragment' [約100行代碼](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/webkit/WebViewFragment.java)。將其複製到您的項目中,將其重構爲您自己的包,並將其修改爲繼承自'support.v4'版本的'Fragment'。我在一個項目上完成了這項工作(尚未發佈,或者我將它指向它),到目前爲止它一直工作正常。 – CommonsWare 2012-03-03 19:47:15
好主意。謝謝。 – JustLearningAgain 2012-03-05 23:13:18