2017-04-23 65 views
1
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); 
    } 
} 

如何意圖MainWeb類..如何在Android中使用Fragment中的按鈕調用頁面?

這我的XML

<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" /> 

片段活動 怎麼辦?

當我按一下按鈕,它給了我一個消息

「Unfortantly應用程序已停止」

+0

什麼是MainWeb.class? –

回答

1

確保您MainWeb Activity類是AndroidManifest.xml中提及。

請勿在您的片段或活動中創建Button或任何UI小部件的靜態實例。這可能會導致內存泄漏。

相關問題