• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How can I use Fragment as a placeholder?

dredd2013

Lurker
I'm getting an error in getFragmentManager() method, here is my code:
[HIGH]


public class WebViewActivity extends Activity{
private WebView _mWebView;
private ViewStub _mViewStub;
private Button _mButton;
private View _mInflated;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
_mViewStub=((ViewStub)findViewById(R.id.southPlachHolder));
_mInflated=_mViewStub.inflate();
Fragment _iFragment=new CustomFragment();
FragmentTransaction _iFragmentTransaction=getFragmentManager().beginTransaction();
_iFragmentTransaction.replace(R.id.fragmentPlachHolder, _iFragment);
_iFragmentTransaction.commit();

_mButton=(Button)findViewById(R.id.loginButton);
_mButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){


}
});

[/HIGH]

And in CustomFragment class I also get an error (supress new api):
[HIGH]
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;

public class CustomFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.subtree, container, false);
}
}

[/HIGH]
 
You cannot use fragment as a place holder.
you should use something like FrameLayout as a fragment_holder
and add your CustomFragment in to it.

(R.id.fragmentPlaceHolder should be a FrameLayout Class instead of fragment class in xml)

and in your 1st time you should call FragmentTransaction.add instead of replace
because your holder is contain no fragment (so the 2nd time .replace is seems OK)

And in your CustomFragment class
i think that (supress new api) is from
import android.app.Fragment >>>>> added in API LEVEL 11
so set your Manifest minsdk to 11
or
import android.support.v4.app.Fragment >>>>> this is the right import for API lower than 11 and want to use fragment

but the next problem is if you use android.support.v4.app.Fragment
you have to change some code in your Activity

first extends FragmentActivity instead of extends Activity

and

try getSupportFragmentManager() instead of getFragmentManager()

hope this help.
 
Back
Top Bottom