27

I was trying to go to another page using button, but it always fail.

Here is my First Class with its XML:

public class FindPeopleFragment extends Fragment {
    public FindPeopleFragment(){}

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

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return rootView;
    }

    public void goToAttract(View v)
    {
        Intent intent = new Intent(getActivity().getApplication(), MainActivityList.class);
        startActivity(intent);
    }
}

Here is my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="48dp"
    android:onClick="goToAttract"
    android:text="Button" /></RelativeLayout>

Here is my stacktrace..this is the result when i used onclicklistener

12-30 16:54:28.006: E/AndroidRuntime(992): FATAL EXCEPTION: main
12-30 16:54:28.006: E/AndroidRuntime(992): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivityList}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.os.Looper.loop(Looper.java:137)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-30 16:54:28.006: E/AndroidRuntime(992):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992):  at java.lang.reflect.Method.invoke(Method.java:511)
12-30 16:54:28.006: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-30 16:54:28.006: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-30 16:54:28.006: E/AndroidRuntime(992):  at dalvik.system.NativeStart.main(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
12-30 16:54:28.006: E/AndroidRuntime(992):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
12-30 16:54:28.006: E/AndroidRuntime(992):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:
AndroidNewbie
  • 669
  • 3
  • 16
  • 23

12 Answers12

61

use this

public void goToAttract(View v)
{
    Intent intent = new Intent(getActivity(), MainActivityList.class);
    startActivity(intent);
}

be sure you've registered MainActivityList in you Manifest

Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
6

Try this code once-

public class FindPeopleFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home,
        container, false);
        Button button = (Button) rootView.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        updateDetail();
        }
        });
        return rootView;
        }

public void updateDetail() {
        Intent intent = new Intent(getActivity(), MainActivityList.class);
        startActivity(intent);
        }
}

And as suggested by Raghunandan remove below code from your fragment_home.xml-

android:onClick="goToAttract"
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
3

Remove this

android:onClick="goToAttract"

Then

View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button b = (Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
     public void onClick(View v)
     {
        Intent intent = new Intent(getActivity(), MainActivityList.class);
        startActivity(intent);

     } 

});
return rootView;

The error says you need to public void goToAttract(View v) in Activity class

Could not find method goToAttract(View) in the activity class

Reason pls check the answer by PareshMayani @

Android app crashing (fragment and xml onclick)

Edit:

Caused by: java.lang.OutOfMemoryError

I guess you have a image that is too big to fit in and it needs to be scaled down. Hence the OutOfMemoryError.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • are you sure that i dont need to do this "Button b = **(Button)** rootView.findViewById(R.id.button1);" i get error without that and after running with that i get this error [link](http://i.stack.imgur.com/pj1sh.png) – AndroidNewbie Dec 30 '13 at 08:58
  • @user3138250 yess you need to cast it to button. it was a typo error – Raghunandan Dec 30 '13 at 08:58
  • @user3138250 pls post the stack trace instead of posting links to snap shots. No way we can guess what's happenning without seeing the stacktrace. If you keep posting links i don't think i willwaste my time looking at snap shots – Raghunandan Dec 30 '13 at 09:01
  • @KanwaljitSingh i rolled back the edit bcoz i din't find anything that needed substantial edit to my post – Raghunandan Dec 30 '13 at 09:03
  • @user3138250 why did you re-edit your post without stacktrace. OutOf Memory generally occurs bcoz you have image that is too large to fit in. – Raghunandan Dec 30 '13 at 09:11
  • @Raghunandan but i am not using image. – AndroidNewbie Dec 30 '13 at 09:13
  • @AndroidNewbie might be using somewhere in your app. Are you sure. – Raghunandan Dec 30 '13 at 15:19
3

in your receiving intent use as

Intent intent = getActivity().getIntent();
        ((TextView)view.findViewById(R.id.hello)).setText(intent.getStringExtra("Hello"));

and in your send intent

Intent intent = new Intent(getActivity(),Main2Activity.class);
        intent.putExtra("Hello","Nisar");
        getActivity().startActivity(intent);

remember both are in fragments

Nisar Ahmad
  • 170
  • 1
  • 7
3

use getContext() instead of MainActivity.this

Intent intent = new Intent(getContext(), SecondActivity.class);
startActivity(start);
UTTAM
  • 319
  • 3
  • 4
2

Hope this code will help

public class ThisFragment extends Fragment {

public Button button = null;
Intent intent;

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

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

    intent = new Intent(getActivity(), GoToThisActivity.class);
    button = (Button) rootView.findViewById(R.id.theButtonid);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });
    return rootView;
}

You can use this code, make sure you change "ThisFragment" as your fragment name, "yourlayout" as the layout name, "GoToThisActivity" change it to which activity do you want and then "theButtonid" change it with your button id you used.

slfan
  • 8,950
  • 115
  • 65
  • 78
IMNK
  • 21
  • 1
1

You need to use getActivity() method from fragment for Intents.

Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
Madhu Jayarama
  • 415
  • 1
  • 5
  • 15
1
 FragmentManager fragmentManager =  getFragmentManager();
 fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();

MySchedule is the name of my java class.

Loofer
  • 6,841
  • 9
  • 61
  • 102
1

For Kotlin you can use

val myIntent = Intent(activity, your_destination_activity::class.java)
startActivity(myIntent)
Boken
  • 4,825
  • 10
  • 32
  • 42
Bikeboy
  • 543
  • 4
  • 5
  • Hi, I tried this and now getting error Unresolved reference for your_destination_activity (in my case RegistrationActivity). Do I need to import it in fragment? When making an intent from 1 activitie to another, I don't need an import, is it different for fragments? Thanks. – akrelj Jan 31 '20 at 12:06
1

If you can get a View in that fragment, you can access the context from there:

view.getContext().startActivity(intent);
Lukas Mohs
  • 290
  • 3
  • 4
1
public class OneWayFragment extends Fragment {

    ImageView img_search;


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


        View view = inflater.inflate(R.layout.one_way_fragment, container, false);
        img_search = (ImageView) view.findViewById(R.id.search);


        img_search.setOnClickListener(new View.OnClickListener() {
            @Override`enter code here`
            public void onClick(View v) {
                Intent displayFlights = new Intent(getActivity(), SelectFlight.class);
                startActivity(displayFlights);
            }
        });
Nick
  • 138,499
  • 22
  • 57
  • 95
bunty785
  • 199
  • 1
  • 9
  • Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. [From Review](https://stackoverflow.com/review/late-answers/25188774) – Nick Jan 26 '20 at 05:01
1

You can use any of these:

Intent i = new Intent(getActivity().getApplication(),SecondActivity.class);

or

Intent i = new Intent(getActivity(),SecondActivity.class);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61