Android 使用按钮使用片段更改布局
所以我试图用一个使用fragment的按钮来改变我的布局,但我无法让它工作,可能是因为我不太理解它是如何工作的。总体来说,我对编程非常陌生,所以我在理解一些东西时有点困难。不管怎样,这就是我想做的 主要活动Android 使用按钮使用片段更改布局,android,android-layout,button,android-fragments,nullpointerexception,Android,Android Layout,Button,Android Fragments,Nullpointerexception,所以我试图用一个使用fragment的按钮来改变我的布局,但我无法让它工作,可能是因为我不太理解它是如何工作的。总体来说,我对编程非常陌生,所以我在理解一些东西时有点困难。不管怎样,这就是我想做的 主要活动 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst
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();
}
}
碎片
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;
}
}
碎片二
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;
}
}
main menu.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>
有没有办法解决这个问题?或者如何使用按钮更改布局?您的按钮splay位于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
希望能有帮助
附言:为了更好地理解片段在这之后似乎仍然会崩溃,同样的错误。结合你在fragmentone和jaydroiders代码中加入onClick的技巧,它工作得很好。非常感谢。内容无法解析。这也不起作用。编辑:我的文字写错了,效果很好!(在“R.id.content”之前添加“android”之后)
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();
}
});
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();
}
});