转化成活性片段活性、转化成、片段

2023-09-12 04:17:23 作者:孤人自嘲

这是一个简单的code玩上点击关闭,按钮一个声音,这code最初写的活动,但现在我想改变它片段。

错误

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

2)的方法创建(背景下,INT)在类型的MediaPlayer不适用的参数(Rajathmusic,INT)。

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

我刚开始了与Android的发展,任何帮助,将AP preciated!

 公共类Rajathmusic扩展片段{

私有静态最后字符串变量=MyActivity;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.activity_main);

    Log.v(TAG初始化声音......);

    最后的MediaPlayer MP = MediaPlayer.create(这一点,R.raw.rajath);

    按钮play_button =(按钮)this.findViewById(R.id.button3);

    play_button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            Log.v(TAG,播放声音......);
            mp.start();
        }
    });
    Log.v(TAG,听起来初始化。);
}}
 

解决方案 片段有一个名为 onCreateView(LayoutInflater,ViewGroup中,捆绑)方法。覆盖它,用充气的布局,并返回视图。 在自创建方法需要一个上下文,用它传递 getActivity() findViewById(INT)可以被称为 getView()。findViewById(R.id.button3) H型高血压有多可怕

下面是一个简单的code:

 公共类Rajathmusic扩展片段{

    私有静态最后字符串变量=MyActivity;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        返回inflater.inflate(R.layout.activity_main,集装箱,假);
    }

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        Log.v(TAG初始化声音......);

        最后的MediaPlayer MP = MediaPlayer.create(getActivity(),R.raw.rajath);

        视图V = getView();

        按钮play_button =(按钮)v.findViewById(R.id.button3);

        play_button.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                Log.v(TAG,播放声音......);
                mp.start();
            }
        });
        Log.v(TAG,听起来初始化。);
    }

}
 

了解更多关于片段生命周期这里知道为什么我已经把code。在 onActivityCreated ,而不是的onCreate

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.

errors

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

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

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

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

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

解决方案

Fragment has a method called onCreateView(LayoutInflater, ViewGroup, Bundle). Override it, inflate using the layout and return the view. Since create method expects a Context, pass it using getActivity() findViewById(int) can be called as 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 Fragment lifecycle here to know why I've put the code in onActivityCreated and not onCreate