嗨,我正在尝试使用导航抽屉制作一个小应用程序。这里我有一个无效的按钮。但我的编译器没有给我任何警告/错误。
public class Fragment_sylhet extends Fragment {
Button jflng;
public View onCreateView(LayoutInflater inflater ,
ViewGroup container , Bundle saveInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_sylhet, container,false);
/*jflng = (Button)rootView.findViewById(R.id.sylhet1);
jflng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),Fragment_jaflong.class));
}
});*/
jflng=(Button) rootview.findViewById(R.id.sylhet1);
jflng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent in = new Intent(getActivity(),Fragment_jaflong.class);
//startActivity(in);
startActivity(new Intent(getActivity(),Fragment_jaflong.class));
}
});
return rootview;
}}
和我的followinf fragment_shylet.xml是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Sylhet Zone"
android:textSize="30dp"
android:gravity="center"
android:id="@+id/sylht"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="@+id/sylht">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Jaflong"
android:id="@+id/sylhet1"
android:background="@drawable/custom_button_one"/>
</RelativeLayout>
我的Fragment_jaflong.class是
public class Fragment_jaflong extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_jaflong, container, false);
return rootview;
}}
/////如果我想通过此按钮进入任何网页,它可以正常工作,但只有当我打电话给这个课程时才会出现干扰。
答案 0 :(得分:0)
您无法启动具有意图的片段。您需要启动一个将启动片段的活动。或者您可以使用片段事务打开片段。请务必使用与当前片段相同的视图。
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment_jaflong frag = new Fragment_jaflong();
ft.replace(R.id.container, frag);
ft.commit();
This is a good guide设置片段交易