使用按钮使用片段来改变布局(Using button to change layout using

2019-10-21 06:51发布

所以我想用片段的按钮来改变我的布局,但我不能得到它的工作,可能是因为我并不真正了解它是如何工作的。 我很新的总体规划的,所以我有一点很难理解一些东西。 不管怎么说,这就是我想做的事情。

MainActivity

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Fragmenttwo thenewfrag = new Fragmenttwo();
    fragmentTransaction.replace(android.R.id.content, thenewfrag);

    Button splay = (Button) findViewById(R.id.splay);

    splay.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
          FragmentManager fragmentManager = getFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

          Fragmentone newfrag = new Fragmentone();
          fragmentTransaction.replace(android.R.id.content, newfrag);

          fragmentTransaction.commit();
    }
});
fragmentTransaction.commit();
}
}

Fragmentone

public class Fragmentone extends Fragment {

 public Fragmentone() {}

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.mainmenu, container, false);
     return view;

    }
}

Fragmenttwo

public class Fragmenttwo extends Fragment{

 public Fragmenttwo() {}

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.start_board, container, false);
     return view;

    }
}

mainmenu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainmenu" >

<Button
    android:id="@+id/splay"
    android:layout_width="195dp"
    android:layout_height="64dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

logcat的(我得到NullPointerException异常的onClickLitsener?错误happends当按下按钮)

E/AndroidRuntime(2001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gerfort.gerfortrps/com.gerfort.gerfortrps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

任何想法怎么办解决这一问题? 或如何使用按钮更改布局?

Answer 1:

Button yourButton = (Button) rootView
            .findViewById(R.id.btnid);
    yourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Fragment newFragment = new YourFragment();
            FragmentTransaction transaction = getFragmentManager()
                    .beginTransaction();

            // Replace whatever is in the fragment_container view with this
            // fragment,
            // and add the transaction to the back stack
            transaction.replace(android.R.id.content, newFragment);
            transaction.addToBackStack("tag");

            // Commit the transaction
            transaction.commitAllowingStateLoss();

        }
    });


Answer 2:

您的按钮扇是mainmenu.xml,这是在FragmentOne加载。 所以,你需要找到这样的参考:

public class Fragmentone extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mainmenu, container, false);
        Button splay = (Button) view.findViewById(R.id.splay);
        //onClick here
        return view;
    }
}

您的按钮张开为空。 所以你得到

E/AndroidRuntime(2001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gerfort.gerfortrps/com.gerfort.gerfortrps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

希望能帮助到你。

PS: 参考更好地了解片段



文章来源: Using button to change layout using Fragment