gpt4 book ai didi

java - 动态设置Fragment时出错

转载 作者:行者123 更新时间:2023-12-01 23:21:12 25 4
gpt4 key购买 nike

我正在尝试动态设置 fragment 。我需要在 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

此错误来自 MainActivity.javaFragmentTransaction ft = getFragmentManager().beginTransaction();

我尝试了 Eclipse 建议的修复,但它没有给我任何帮助。当我使用 exlipse 建议修复错误时,它给出了另一个错误。当我使用 Eclipse 建议修复它(单击 Eclipse 建议)时,它会返回到之前的错误。

我在这里做什么?

最佳答案

尝试使用getSupportFragmentManager()而不是getFragmentManager()

关于java - 动态设置Fragment时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com