我正在尝试动态设置片段。我需要在FragmentActivity
中完成。这是我尝试过的。
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment frag = new InputFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</RelativeLayout>
InputFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class InputFragment extends Fragment {
private Button submit;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.input, container,false);
submit = (Button)view.findViewById(R.id.input_submit_button);
submit.setOnClickListener(new SubmitButtonAction());
return view;
}
private class SubmitButtonAction implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment frag = new OutputFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
}
}
存在编译错误。在这里。
Type mismatch: cannot convert from android.app.FragmentTransaction to android.support.v4.app.FragmentTransaction
此错误来自FragmentTransaction ft = getFragmentManager().beginTransaction();
MainActivity.java
我尝试过Eclipse提出的修复程序,但它没有给我任何帮助。当我使用exlipse建议修复错误时,它会产生另一个错误。当我使用eclipse建议(点击eclipse建议)修复它时,它会回到之前的错误。
我在这做什么wromg?
答案 0 :(得分:1)
尝试使用getSupportFragmentManager()
代替getFragmentManager()
答案 1 :(得分:1)
如果您想在Android sdk vrsion 3.0下使用片段,请使用支持4支持lib中提供的getSupportFragmentManager()方法。 (确保将supportv4 lib放在lib文件夹中并添加项目构建路径)
答案 2 :(得分:1)
我不确定您是否可以使用支持库进行转换。一些建议:
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
extend Activity
并使用getFragmentManager()