Android 从片段到活动的意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20835933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:33:51  来源:igfitidea点击:

Intent from Fragment to Activity

androidxmlandroid-fragmentsfragment

提问by AndroidNewbie

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

我试图使用 转到另一个页面button,但它总是失败。

Here is my First Class with its XML:

这是我的头等舱及其 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:

这是我的 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

这是我的堆栈跟踪..这是我使用 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.access0(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:

回答by Nitin Misra

use this

用这个

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

be sure you've registered MainActivityListin you Manifest

确保您已MainActivityList在 Manifest 中注册

回答by Kanwaljit Singh

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-

根据 Raghunandan 的建议,从您的fragment_home.xml- 中删除以下代码

android:onClick="goToAttract"

回答by Raghunandan

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)在 Activity 类中

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

Reason pls check the answer by PareshMayani @

原因请检查 PareshMayani @ 的答案

Android app crashing (fragment and xml onclick)

Android 应用程序崩溃(片段和 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

回答by Nisar Ahmad

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

记住两者都是碎片

回答by Bikeboy

For Kotlin you can use...

对于 Kotlin,您可以使用...

val myIntent = Intent(activity, your_destination_activity::class.java)
startActivity(myIntent)

回答by IMNK

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.

您可以使用此代码,确保将“ ThisFragment”更改为您的片段名称,“ yourlayout”作为布局名称,“ GoToThisActivity”将其更改为您想要的活动,然后“ theButtonid”将其更改为您使用的按钮ID .

回答by Lukas Mohs

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

如果您可以View在该片段中获得 a ,则可以从那里访问上下文:

view.getContext().startActivity(intent);

回答by bunty785

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);
            }
        });

回答by Madhu Jayarama

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

您需要将片段中的 getActivity() 方法用于 Intent。

Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);

回答by user9518168

 FragmentManager fragmentManager =  getFragmentManager();
 fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();

MySchedule is the name of my java class.

MySchedule 是我的 java 类的名称。