简体   繁体   中英

Open Another Activity Using Fragment Button

Okay so I've tried two types of code to get this to work and it keeps giving me force closes when I press the button to go into another Activity. I'm using a Fragment and there's a button in that Fragments code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.

Here's the Java code:

1st Method

public class About extends Fragment {

    Intent intent;

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

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

        intent = new Intent(getActivity(), Contact_Developer.class);
        final Button button = (Button) rootView.findViewById(R.id.btnContactDev);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        return rootView;
    }
}

2nd Method

public class About extends Fragment {

    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

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

        Button button = (Button) rootView.findViewById(R.id.btnContactDev);
        button.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent(getActivity(), Contact_Developer.class);
              getActivity().startActivity(intent);
           }
        });
        return rootView;
    }
}

I really don't know what's going on and why I'm getting force closes but if anyone could help me and explain a little what I did wrong that'd be more than enough

Do not handle the onClick for the fragment button in the Fragment. Let it go it's parent activity. And start the activity from the parent activity.

To make sure that the button onClick event is sent to the parent activity, make sure, in your about.xml , for the button with id btnContactDev , you have the following parameter:

<Button android:id="@+id/btnContactDev"
  android:onClick="buttonClick"
  ...
/>

and in your parent activity (parent of About fragment), you have:

public void buttonClick(View v) {
  switch(v.getId()) {
    case R.id.btnContactDev:
      Intent myIntent = new Intent();
      myIntent.setClassName(your_package_name_string, your_activity_name_string);
      // for ex: your package name can be "com.example"
      // your activity name will be "com.example.Contact_Developer"
      startActivity(myIntent);
    break;
  }
}

HTH.

PS: This solution is very specific for what your requirement. In general, it's best to handle the onClick events related to the fragment inside the fragment class.

PS: Yes, as the other solution says, make sure you have registered the Contact_Developer Activity in your Manifest file.

Have you declared the proper activity in the AndroidManifest.xml? Each activity other then the first should be declared inside the application tag, like this:

<activity
   android:name=".Contact_Developer"
   android:label="@string/app_name" >
</activity>

If it's not found, it will give force close.

I think the issue here is that the activity is not ready.

I would not recommend you following harikris's solution even though it works. It's usually bad practice to put code ( onClick() which handles the click event) in your XML file. For example, it will be very difficult to hunt that piece of code down to review/ change it in the future yourself, not to mention another developer in your team.

I'd suggest to listen to the event the activity is ready ie onActivityCreated() within your Fragment class. Override the implementation of the same method and call something in your activity eg onFragmentReady() like in the example below.

Fragment class :

public class About extends Fragment {

    Intent intent;

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

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

        intent = new Intent(getActivity(), Contact_Developer.class);
        final Button button = (Button) rootView.findViewById(R.id.btnContactDev);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        return rootView;
    }

    @Override
        public void onActivityCreated (Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            ((MyActivity) this.getActivity()).onFragmentReady();
        } 
}  

Activity class :

public class MyActivity extends FragmentActivity {
    private void onFragmentReady() {
        Log.i(TAG, "testing");
         intent = new Intent(getActivity(), Contact_Developer.class);
         final Button button = (Button) rootView.findViewById(R.id.btnContactDev);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    public class About extends Fragment {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM