Intent from Fragment to Activity

后端 未结 11 1488
再見小時候
再見小時候 2021-02-03 23:22

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

Here is my First Class with its XML:

public class Fin         


        
相关标签:
11条回答
  • 2021-02-04 00:07

    use getContext() instead of MainActivity.this

    Intent intent = new Intent(getContext(), SecondActivity.class);
    startActivity(start);
    
    0 讨论(0)
  • 2021-02-04 00:08

    For Kotlin you can use

    val myIntent = Intent(activity, your_destination_activity::class.java)
    startActivity(myIntent)
    
    0 讨论(0)
  • 2021-02-04 00:10

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

    Intent intent = new Intent(getActivity(), SecondActivity.class);
    startActivity(intent);
    
    0 讨论(0)
  • 2021-02-04 00:12

    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

    0 讨论(0)
  • 2021-02-04 00:14

    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.

    0 讨论(0)
提交回复
热议问题