【发布时间】:2016-09-12 06:14:24
【问题描述】:
您好,我正在尝试制作一个使用两个不同类的应用程序,我知道我不能在 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;
}}
【问题讨论】:
-
扩展一个Activity和Fragment的目的是什么?它们是 Android 中的独立构造。通过尝试从这两个父母那里汲取灵感,您究竟想结合什么功能?
-
我希望能够滑动导航应用,应用使用MediaPlayer播放声音
标签: java android android-fragments android-studio appcompatactivity