如何在Android中使用Fragment中的按钮调用页面?
如何创建MainWeb类 这是我的XML如何在Android中使用Fragment中的按钮调用页面?,android,android-fragments,Android,Android Fragments,如何创建MainWeb类 这是我的XML public class Menu1 extends Fragment implements View.OnClickListener { public static Button button_sbm; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullabl
public class Menu1 extends Fragment implements View.OnClickListener {
public static Button button_sbm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
/* return inflater.inflate(R.layout.fragment_menu_1, container, false);*/
View myView = inflater.inflate(R.layout.fragment_menu_1, container, false);
button_sbm = (Button) myView.findViewById(R.id.button);
button_sbm.setOnClickListener(this);
return myView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Home");
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainWeb.class);
startActivity(intent);
}
}
碎片到活动
怎么办
当我点击按钮时,它会给我一条信息
“不幸的是,应用程序已停止”
确保AndroidManifest.xml中提到了您的MainWeb活动类
不要在片段或活动中创建按钮或任何UI小部件的静态实例。这可能会导致内存泄漏。什么是MainWeb.class?
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/yoga"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="Goto Website"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />