我试图从一个片段切换到另一个片段

时间:2017-05-12 13:36:47

标签: android android-fragments

我正在尝试从一个片段移动/切换到另一个片段但是当我这样做时Android应用程序停止不幸 我已经附上了所有的代码文件。 请告诉我该怎么办

MainActivity.java

package com.example.mnaum.myapplication;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.text.TextDirectionHeuristicCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private Button button1, button2, button3;
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    listeners();
}

private void listeners() {
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentA fragmentA=new FragmentA();
            transaction.add(R.id.framel,fragmentA);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentB fragmentB=new FragmentB();
            transaction.replace(R.id.framel,fragmentB);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

private void init() {
    button1=(Button)findViewById(R.id.btn1);
    button2=(Button)findViewById(R.id.btn2);
    button3=(Button)findViewById(R.id.btn3);

}
}

FragmentA.java

package com.example.mnaum.myapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_fragment_a,null);
    return view;
}
}

FragmentB.java

package com.example.mnaum.myapplication;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentB extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_fragment_b,null);
    return view;
}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mnaum.myapplication.MainActivity">
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:id="@+id/linLayout"
   android:weightSum="3">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight="1"
    android:id="@+id/btn1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button 2"
    android:id="@+id/btn2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight="1"
    android:id="@+id/btn3"/>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_below="@+id/linLayout"
    android:id="@+id/framel"
    android:layout_height="400dp"
    >

</FrameLayout>

</RelativeLayout>

activity_fragment_a

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0ff"
tools:context="com.example.mnaum.myapplication.FragmentA">

activity_fragment_b

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mnaum.myapplication.FragmentB"
android:background="#f0f">
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

我为您编写了类似的代码。

   private void AddBottomNavigation() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = new HomeFragment();
        final FragmentTransaction transaction = 
        fragmentManager.beginTransaction();
        transaction.add(R.id.main_container, fragment).commit();
       BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.bottom_navigation);
      bottomNavigationView.setOnNavigationItemSelectedListener(new 
 BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.action_home:
                    fragment = new HomeFragment();
                    break;
                case R.id.action_charge:
                    fragment = new ChargeFragment();
                    break;
                case R.id.action_rank:
                    fragment = new RankFragment();
                    break;
            }
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_container, fragment).commit();
            return true;
        }
    });

}

希望这会对你有所帮助。

相关问题