Intent from Fragment to Activity


Intent from Fragment to Activity
I was trying to go to another page using button
, but it always fail.
button
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 ComponentInfoinfo.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:
pardon? i dont get your point.
– AndroidNewbie
Dec 30 '13 at 8:45
Intent() need an activity instance, and a class, so GetActivity() for the instance and ClassName.class for the class. Getapplication() return an instance of the Application variable.
– An-droid
Dec 30 '13 at 8:47
@user3138250 have you tried the code provided by raghunandan?
– Kanwaljit Singh
Dec 30 '13 at 9:31
@KanwaljitSingh yes, still it doesnt work with my problem
– AndroidNewbie
Dec 30 '13 at 9:36
7 Answers
7
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
MainActivityList
not working. link i always get this error
– AndroidNewbie
Dec 30 '13 at 8:44
@user3138250 post you code for button
– Nitin Misra
Dec 30 '13 at 8:47
above already..in XML i used the android:onClick"method_name" and in java fragment class i have the method "goToAttract"
– AndroidNewbie
Dec 30 '13 at 8:51
@user3138250 refer to raghunandan's answer
– Nitin Misra
Dec 30 '13 at 8:53
Sir, you should have put be sure you've registered MainActivityList in you Manifest first. Thanks. Finally working.
– Dmytro I.
Jul 1 '17 at 9:38
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
-
fragment_home.xml
android:onClick="goToAttract"
your code, try to intend it correctly it does not look well
– maytham-ɯɐɥʇʎɐɯ
Feb 25 '16 at 23:09
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
public void goToAttract(View v)
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
.
OutOfMemoryError
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
– AndroidNewbie
Dec 30 '13 at 8:58
@user3138250 yess you need to cast it to button. it was a typo error
– Raghunandan
Dec 30 '13 at 8: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 9:01
nice link to learn new thing.
– Kanwaljit Singh
Dec 30 '13 at 9:02
@KanwaljitSingh i rolled back the edit bcoz i din't find anything that needed substantial edit to my post
– Raghunandan
Dec 30 '13 at 9:03
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
You need to use getActivity() method from fragment for Intents.
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();
MySchedule is the name of my java class.
For Kotlin you can use...
val myIntent = Intent(activity, your_destination_activity::class.java)
startActivity(myIntent)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
getActivity().getApplication() should be getActivity() to begin with
– An-droid
Dec 30 '13 at 8:43