Android 将活动转换为片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21205732/
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 04:08:31  来源:igfitidea点击:

converting activity into fragment

androidandroid-fragmentsandroid-activity

提问by Rajath

This is a simple code to play a sound on click off a button, this code was initially written in Activity but now i want to change it to Fragments.

这是一个在点击 a 时播放声音的简单代码button,该代码最初是在 Activity 中编写的,但现在我想将其更改为 Fragments。

errors

错误

1) The method setContentView(int)is undefined for the type Rajathmusic.

1)setContentView(int)未定义 Rajathmusic 类型的方法。

2) The method create(Context, int)in the type MediaPlayer is not applicable for the arguments (Rajathmusic, int).

2) create(Context, int)MediaPlayer 类型中的方法不适用于参数 (Rajathmusic, int)。

3)The method findViewById(int)is undefined for the type Rajathmusic.

3)findViewById(int)未定义 Rajathmusic 类型的方法。

I am just starting off with android development, any help would be appreciated!

我刚刚开始使用 android 开发,任何帮助将不胜感激!

public class Rajathmusic extends Fragment {

private static final String TAG = "MyActivity";

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

    setContentView(R.layout.activity_main);

    Log.v(TAG, "Initializing sounds...");

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.rajath);

    Button play_button = (Button)this.findViewById(R.id.button3);

    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "Playing sound...");
            mp.start();
        }
    });
    Log.v(TAG, "Sounds initialized.");
}}

回答by Ganesh Bhambarkar

  1. Fragmenthas a method called onCreateView(LayoutInflater, ViewGroup, Bundle). Override it, inflate using the layout and return the view.
  2. Since create method expects a Context, pass it using getActivity()
  3. findViewById(int)can be called as getView().findViewById(R.id.button3)
  1. Fragment有一个方法叫做onCreateView(LayoutInflater, ViewGroup, Bundle). 覆盖它,使用布局膨胀并返回视图。
  2. 由于 create 方法需要 a Context,因此使用getActivity()
  3. findViewById(int)可以称为 getView().findViewById(R.id.button3)

Here is a sample code:

这是一个示例代码:

public class Rajathmusic extends Fragment {

    private static final String TAG = "MyActivity";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.rajath);

        View v = getView();

        Button play_button = (Button) v.findViewById(R.id.button3);

        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

}

Read more about Fragmentlifecycle hereto know why I've put the code in onActivityCreatedand not onCreate

在此处阅读有关Fragment生命周期的更多信息以了解为什么我将代码放入而不是onActivityCreatedonCreate

回答by Dra?ko

Use this code in overriden method OnActivityCreated(...)and instead of thisuse getActivity(), since new fragment is not activity any more.

在覆盖方法中使用此代码OnActivityCreated(...)而不是this使用getActivity(),因为新片段不再是活动。

回答by Kanwaljit Singh

In fragment onCreate method is usually written as-

在片段 onCreate 方法中通常写为 -

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

And Use-

并使用-

final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.rajath);
Button play_button = (Button) view.findViewById(R.id.button3);

If you want to read more about fragments. Check this link.

如果您想阅读有关片段的更多信息。检查此链接。

回答by Bilal

Copy the code from the onCreate()method from the activity and paste it into the onCreateView()method of the fragment.

onCreate()从活动的方法中复制代码并将其粘贴到onCreateView()片段的方法中。

To fix the errors, pass getActivityin place of this keyword and use getView().findViewById().

要修复错误,请getActivity代替此关键字并使用getView().findViewById().