java 一起使用 Fragment 和 AppCompatActivity 类

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

Using Fragment and AppCompatActivity classes together

javaandroidandroid-fragmentsandroid-studioappcompatactivity

提问by Ollie Atkins

Hi I'm trying to make an app which uses two different classes I'm aware I can't use extend with two classes in Java. How would I go about separating the below code into two different classes so one can extend Fragment and the other AppCompatActivity?

嗨,我正在尝试制作一个使用两个不同类的应用程序,我知道我不能使用 Java 中的两个类来扩展。我将如何将下面的代码分成两个不同的类,以便一个可以扩展 Fragment 和另一个 AppCompatActivity?

package com.example.oliver.myapplication;

import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.Random;


public class MyFragment extends AppCompatActivity, Fragment {

    Button b, b2;
    MediaPlayer nice, burp;
    ImageButton img;
    int n;
    MediaPlayer [] s = new MediaPlayer[6];
    AlertDialog.Builder adb;

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.my_fragment, container, false);
            img = (ImageButton) rootView.findViewById(R.id.img);

            s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
            s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
            s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);



        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < 1; i++) {
                    Random r = new Random();
                    n = r.nextInt(3);

                    s[n].start();
                }
            }
        });return rootView;


}}

回答by Karthiksrndrn

The above code is not valid Java.

上面的代码不是有效的 Java。

A class can only extend one class. You activity/fragment is extending 2 classes at the same time.

一个类只能扩展一个类。您的活动/片段同时扩展了 2 个类。

The class given above is a valid Fragment subclass, except for these lines :

上面给出的类是一个有效的 Fragment 子类,除了以下几行:

        s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
        s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
        s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);

Replace "MyFragment.this" with "MyFragment.getActivity()".

将“MyFragment.this”替换为“MyFragment.getActivity()”。

Now you can replace "extends AppCompatActivity, Fragment" with "extends Fragment"

现在您可以将“extends AppCompatActivity, Fragment”替换为“extends Fragment”

Now you have a valid Fragment.

现在你有了一个有效的 Fragment。

Create the AppCompatActivity yourself. Look up how you can add a fragment to an activity.

自己创建 AppCompatActivity。查看如何将片段添加到活动中。

As a tip in good programming practice, I suggested that you move the following code in onCreateView() to onActivityCreated() :

作为良好编程实践的提示,我建议您将 onCreateView() 中的以下代码移动到 onActivityCreated() :

        img = (ImageButton) rootView.findViewById(R.id.img);

        s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
        s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
        s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);



    img.setOnClickListener(new View.OnClickListener() {
        @Override 
        public void onClick(View view) {
            for (int i = 0; i < 1; i++) {
                Random r = new Random();
                n = r.nextInt(3);

                s[n].start(); 
            } 
        } 
    });

Change

改变

        img = (ImageButton) rootView.findViewById(R.id.img);

to

        img = (ImageButton) getView().findViewById(R.id.img);