Android 如何让一个活动打开一个片段?

Android 如何让一个活动打开一个片段?,android,android-intent,android-fragments,android-activity,Android,Android Intent,Android Fragments,Android Activity,我有一个名为Chap.java的片段和一个名为Exercise.java的活动。 我成功地获得了Chap.java片段以打开activity Exercise.java。 但是有没有办法让我反过来做呢? 我的意思是让活动打开碎片 以下是我当前的代码: 我的Chap.java package com.boszcorp.newbuttontestcrezi; import android.app.AlertDialog; import android.content.DialogInterface

我有一个名为Chap.java的片段和一个名为Exercise.java的活动。 我成功地获得了Chap.java片段以打开activity Exercise.java。 但是有没有办法让我反过来做呢? 我的意思是让活动打开碎片

以下是我当前的代码:

我的Chap.java

package com.boszcorp.newbuttontestcrezi;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import static com.boszcorp.newbuttontestcrezi.R.layout.chapter;

/**
 * Created by Aizad on 23/03/2015.
 */
public class Chap extends Fragment {
    View rootview;

    @Nullable

    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(chapter, container, false);
        Button newPage = (Button)rootview.findViewById(R.id.buttonEx);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
        Intent intent = new Intent(getActivity(), Exercise.class);
        startActivity(intent);




    }
});
        return rootview;};
}
My Exercise.java

package com.boszcorp.newbuttontestcrezi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by Aizad on 23/03/2015.
 */
public class Exercise extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise);
        Button newReturn = (Button)findViewById(R.id.buttonChap);
        newReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (Exercise.this, Chap.class);
                startActivity(intent);
            }
        });


    }
}

在onClick()中使用以下代码:


希望这能奏效。

是的,你可以。试试这个:

Chap fragment = new Chap();
FragmentManager fm = getSupportFragmentManager(); //or getFragmentManager() if you are not using support library.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.your_container, fragment);
ft.commit();

希望这有帮助。

您可以这样做,创建一个扩展活动的新类,并在新类中执行以下操作:

将新类的内容视图设置为fragment的布局,并添加以下代码:

Chap f1 = new Chap();       
getSupportFragmentManager().beginTransaction().add(R.id.fragment_detail, f1).commit();

希望这对您有所帮助。

我能知道吗。你的具体问题是什么。你无法从活动中打开片段?@Kesh1234是的,我正在尝试从活动中打开片段,但当我按下按钮时,应用程序会自动关闭crashes@Sakitaro你能发布StackTrace(R.id.content\U框架)我应该填充什么使用?这是您的FrameLayout id。我不懂SRI,希望您在xml文件中使用这种类型的代码:它的下划线是扭曲的红线,上面写着“无法应用”
Chap f1 = new Chap();       
getSupportFragmentManager().beginTransaction().add(R.id.fragment_detail, f1).commit();