Android Can';t将WebViewFragment派生类添加到FragmentTransaction
我正在使用ActionBarSherlock并实现一个选项卡式应用程序。每个选项卡表示一个片段,其中只包含一个WebView。我已经用从Fragment派生的对象实现了它。但当我将其更改为WebViewFragment时,我无法再将其添加到FragmentTransaction中。我想知道我是否导入了正确的东西?代码如下:Android Can';t将WebViewFragment派生类添加到FragmentTransaction,android,actionbarsherlock,Android,Actionbarsherlock,我正在使用ActionBarSherlock并实现一个选项卡式应用程序。每个选项卡表示一个片段,其中只包含一个WebView。我已经用从Fragment派生的对象实现了它。但当我将其更改为WebViewFragment时,我无法再将其添加到FragmentTransaction中。我想知道我是否导入了正确的东西?代码如下: import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity;
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扩展了WebViewFragment,后者扩展了片段。FragmentTransaction方法应该将MyWebviewFragment视为一个简单的片段。就像我之前说的,这可能与我的进口有关吗
谢谢
MyWebviewFragment扩展了WebViewFragment,后者扩展了片段
您不能混合使用API级别11的本机片段(android.app.Fragment
)和android支持包片段(android.Support.v4.app.Fragment
)。您无法创建android.webkit.WebViewFragment
并将其与android.support.v4.app.FragmentActivity
一起使用,因为android.webkit.WebViewFragment
扩展了android.app.Fragment
不要通过创建API Level 11+应用程序来使用ActionBarSherlock和Android支持包,或者不要使用
WebViewFragment
(或者从源代码中复制并重构到您的项目中)。我担心这一点。我希望他们尽快将WebViewFragment添加到Android支持包中。对于那些希望在所有平台上运行但仍使用片段的人来说,这是一个很大的漏洞。@justlearning:WebViewFragment
是。将其复制到您的项目中,重构到您自己的包中,并将其更改为继承自support.v4
edition的Fragment
。我在一个项目上做了这件事(还没有发布,或者我会指给你看),到目前为止,它工作得很好。