我创建了一个带有标签的项目,其中包含MainActivity.java和activity_main.xml。我有工具栏,广告和浮动动作按钮。在另一个名为tab1.java的java文件中,我有tab1.xml和一些在单击时播放声音的ImageButtons。问题是,如果我只运行选项卡1(不是整个项目),它可以工作,但如果我运行整个项目,它将不会播放任何声音(按钮工作,但没有任何反应)。 我不知道我应该把这部分代码放在这里,所以你可以问我,我会编辑这个问题。非常感谢你! :)
Ps:抱歉,如果我拼错了一些东西。不是我的母语。
MainActivity.java:
package com.example.mateuspc1.memepocket;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //Banner
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //Interstitial
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
// Code to be executed when an ad finishes loading.
}
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
}
});
TabLayout tabLayout = findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void setSupportActionBar(Toolbar supportActionBar) {
this.toolbar = supportActionBar;
}
public static class Tab1 extends Fragment {
public Tab1() { }
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false); }
}
public static class Tab2 extends Fragment {
public Tab2() { }
/**
* Returns a new instance of this fragment for the given section
* number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false); }
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragmen.t. (defined as a static inner class below).
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
default:
return null;
}
}
@Override
public int getCount() {
return 2; }
}
}
tab1.java:
package com.example.mateuspc1.memepocket;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.util.Objects;
/**
* Created by MateusPC1 on 04/03/2018.
*/
public class tab1 extends MainActivity {
private final String gtaintro = "gtaintro";
private final String gtaagaraga = "gtaagaraga";
private final String bolsdaqeut = "bolsdaqeut";
private final String vitasblblblahahah = "vitasblblblahahah";
private final String vitasuoah = "vitasuoah";
private final String gabedogbark = "gabedogbark";
private final String bolsetemqsef = "bolsetemqsef";
private final String acertomizeravi = "acertomizeravi";
private final String aiaichoque = "aiaichoque";
private final String bnsetimaarte = "bnsetimaarte";
private final String fausterou = "fausterou";
private final String fausttapegfog = "fausttapegfog";
private ImageView imgPicture;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
final ImageButton vitasbut = findViewById(R.id.vitasbut);
final ImageButton vitasbut2 = findViewById(R.id.vitasbut2);
final ImageButton gtabut = findViewById(R.id.gtabut);
final ImageButton gtabut2 = findViewById(R.id.gtabut2);
final ImageButton gabebut = findViewById(R.id.gabebut);
final ImageButton bolsbut = findViewById(R.id.bolsbut);
final ImageButton bolsbut2 = findViewById(R.id.bolsbut2);
final ImageButton acertomizbut = findViewById(R.id.acertomizbut);
final ImageButton aiaichoquebut = findViewById(R.id.aiaichoquebut);
final ImageButton bnsetimaartebut = findViewById(R.id.bnsetimaartebut);
final ImageButton fausteroubut = findViewById(R.id.fausteroubut);
final ImageButton fausttapegfogbut = findViewById(R.id.fausttapegfogbut);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
vitasbut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
vitasbut.setImageResource(R.drawable.vitas1);
play(vitasblblblahahah);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vitasbut.setImageResource(R.drawable.vitas1e2);
}
});
}
});
vitasbut2.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
vitasbut2.setImageResource(R.drawable.vitas2);
play(vitasuoah);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vitasbut2.setImageResource(R.drawable.vitas2e2);
}
});
}
});
gtabut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
gtabut.setImageResource(R.drawable.gta1);
play(gtaintro);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
gtabut.setImageResource(R.drawable.gta1e2);
}
});
}
});
gtabut2.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
gtabut2.setImageResource(R.drawable.gta2);
play(gtaagaraga);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
gtabut2.setImageResource(R.drawable.gta2e2);
}
});
}
});
gabebut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
gabebut.setImageResource(R.drawable.gabe1);
play(gabedogbark);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
gabebut.setImageResource(R.drawable.gabe1e2);
}
});
}
});
bolsbut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
bolsbut.setImageResource(R.drawable.bols1);
play(bolsdaqeut);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
bolsbut.setImageResource(R.drawable.bols1e2);
}
});
}
});
bolsbut2.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
bolsbut2.setImageResource(R.drawable.bols2);
play(bolsetemqsef);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
bolsbut2.setImageResource(R.drawable.bols2e2);
}
});
}
});
acertomizbut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
acertomizbut.setImageResource(R.drawable.acertmiz1);
play(acertomizeravi);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
acertomizbut.setImageResource(R.drawable.acertmiz1e2);
}
});
}
});
aiaichoquebut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
aiaichoquebut.setImageResource(R.drawable.aiaichoque1);
play(aiaichoque);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
aiaichoquebut.setImageResource(R.drawable.aiaichoque1e2);
}
});
}
});
bnsetimaartebut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
bnsetimaartebut.setImageResource(R.drawable.bnsetimaarte1);
play(bnsetimaarte);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
bnsetimaartebut.setImageResource(R.drawable.bnsetimaarte1e2);
}
});
}
});
fausteroubut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
fausteroubut.setImageResource(R.drawable.fausterou1);
play(fausterou);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
fausteroubut.setImageResource(R.drawable.fausterou1e2);
}
});
}
});
fausttapegfogbut.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
fausttapegfogbut.setImageResource(R.drawable.faustfogo1);
play(fausttapegfog);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
fausttapegfogbut.setImageResource(R.drawable.faustfogo1e2);
}
});
}
});
}
private void play(String theText) {
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Objects.equals(theText, gtaintro))
mediaPlayer = MediaPlayer.create(this, R.raw.gtaintro);
else if (Objects.equals(theText, gtaagaraga))
mediaPlayer = MediaPlayer.create(this, R.raw.gtaagaraga);
else if (Objects.equals(theText, bolsdaqeut))
mediaPlayer = MediaPlayer.create(this, R.raw.bolsdaqeut);
else if (Objects.equals(theText, vitasblblblahahah))
mediaPlayer = MediaPlayer.create(this, R.raw.vitasblblblahahah);
else if (Objects.equals(theText, vitasuoah))
mediaPlayer = MediaPlayer.create(this, R.raw.vitasuoah);
else if (Objects.equals(theText, gabedogbark))
mediaPlayer = MediaPlayer.create(this, R.raw.gabedogbark);
else if (Objects.equals(theText, bolsdaqeut))
mediaPlayer = MediaPlayer.create(this, R.raw.bolsdaqeut);
else if (Objects.equals(theText, bolsetemqsef))
mediaPlayer = MediaPlayer.create(this, R.raw.bolsetemqsef);
else if (Objects.equals(theText, acertomizeravi))
mediaPlayer = MediaPlayer.create(this, R.raw.acertomizeravi);
else if (Objects.equals(theText, aiaichoque))
mediaPlayer = MediaPlayer.create(this, R.raw.aiaichoque);
else if (Objects.equals(theText, bnsetimaarte))
mediaPlayer = MediaPlayer.create(this, R.raw.bnsetimaarte);
else if (Objects.equals(theText, fausterou))
mediaPlayer = MediaPlayer.create(this, R.raw.fausterou);
else if (Objects.equals(theText, fausttapegfog))
mediaPlayer = MediaPlayer.create(this, R.raw.fausttapegfog);
}
assert mediaPlayer != null;
mediaPlayer.start();
}
}
答案 0 :(得分:0)
在这段代码中
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
}
我看到您尝试将mediaplayer
设置为null
,但release
仅释放资源文件,您应该将mediaplayer
实例明确设置为null
所以它看起来像这样
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
我希望这是问题所在。我也没有完全掌握你的项目结构。你说你有一个标签。这意味着您使用ViewPager
与TabLayout
一起使用?无论如何,如果你的标签名为fragment
或acivity
,那么这个onclicklistener的代码应该放在tab1.java
中,因为它包含按钮。然后,如果您的启动器活动为MainActivity
,则应转到tab1
至intent
,然后在tab1
与mediaplayer
合作。如果有其他问题,请提出更多问题
编辑:
发布代码之后,很明显媒体播放器的内容非常好,这就是为什么它在单独加载时可以正常工作的原因。但真正的问题是项目的结构。如果你想拥有一个带有viewpager的屏幕并切换标签,那么你应该将tab1.class中的所有代码都放在tab1片段中。这两个片段应该是单独的类,并像现在一样扩展Fragment。但即使你像现在这样离开它们,你需要将所有代码形式的tab1.class文件移动到tab1片段,将xml移动到tab1布局,然后你就可以播放媒体播放器了。
现在发生了什么......你将tab1片段加载到viewpager,它没有按钮的onClicklisteners。然后你不能播放声音,因为逻辑在tab1 Activity中,它应该在tab1片段中。您不需要具有相同名称的活动只需在片段中编写onClick逻辑,它就可以正常工作