Question

friends i have written the code for to display facebook url of apple. I have used fragment in my code

package com.coded.sandeep;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BioFragment extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.bio, container, false);

    return rootView;
}}

to this above code i have to add button clicked event so i have added like this below but it:

is not working i am not able to use onclick event correctly can anyone help me to modify the code

package com.coded.sandeep;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class MainFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_am, container, false);

    return rootView;
}}  
@Override
public void onCreate(Bundle savedInstanceState)
        super.onCreate(savedInstanceState);

        ImageButton amelia_facebook = (ImageButton)rootView.findViewById(R.id.facebook_am);
        amelia_facebook.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),Facebook.class));
            }

        });

    }}

please help me in to above code as i new to android

Was it helpful?

Solution

Initialize in onCreateview

ImageButton btnEnter = (ImageButton)rootView.findViewById(R.id.facebook1);

And move the code related to onCreateView. The button probably belongs to fragment layout

Secondly you inflate activity_main.xml. I am not sure if you inflate the right layout coz it looks like the layout belongs to MainActivity.

Edit:

public class MainFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_am, container, false);
    ImageButton amelia_facebook = (ImageButton)rootView.findViewById(R.id.facebook_am);
    amelia_facebook.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(),Facebook.class));
        }

    });
    return rootView;
}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top