我有个问题要解决:
我正在研究Android Studio中的一个程序,该程序实质上循环播放故事板列表(如幻灯片显示),并且正在使用viewPager组件来执行此操作。这有点酷,因为我不需要按钮。我可以滑动(尽管我正在设置),但是,我也尝试将声音中的幻灯片(在本例中为情节提要)链接起来。问题是该应用程序崩溃了,并且在logcat中给了我一些空错误。这是我到目前为止所做的。 。 。
我已经创建了slideAdapter活动:
package com.example.******.*************;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class SlideAdapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
// List of Images
public int[] lst_storyboard = {
R.drawable.imgmob02, R.drawable.imgmob03, R.drawable.imgmob04,
R.drawable.imgmob05, R.drawable.imgmob06, R.drawable.imgmob07,
R.drawable.imgmob08, R.drawable.imgmob09, R.drawable.imgmob010,
R.drawable.imgmob011,R.drawable.imgmob012,R.drawable.imgmob013
};
// List of mp3 raw audio
public int[] lst_audio_eng = {
R.raw.audio_eng_01, R.raw.audio_eng_02, R.raw.audio_eng_03,
R.raw.audio_eng_04, R.raw.audio_eng_05, R.raw.audio_eng_06,
R.raw.audio_eng_07, R.raw.audio_eng_08, R.raw.audio_eng_09,
R.raw.audio_eng_010, R.raw.audio_eng_011, R.raw.audio_eng_012
};
public SlideAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return lst_storyboard.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_slide_adapter,container,false);
LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
ImageView imgslide = (ImageView) view.findViewById(R.id.slideimg);
imgslide.setImageResource(lst_storyboard[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
这是slideactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/slidelinearlayout">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/slideimg"
android:contentDescription="@string/todo" />
</LinearLayout>
这是mainActivity文件:
package com.example.******.***********;
import android.media.MediaPlayer;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import static com.example.gman5541.childrensbookconcept2.SlideAdapter.lst_audio_eng;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private SlideAdapter myadapter;
private MediaPlayer mp;
private MediaPlayer BG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myadapter = new SlideAdapter(this);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.addOnPageChangeListener(player);
viewPager.setAdapter(myadapter);
viewPager.setCurrentItem(0);
BG = MediaPlayer.create(getApplicationContext(), R.raw.audio_eng_01);
BG.setLooping(true);
BG.setVolume(100, 100);
BG.start();
mp = MediaPlayer.create(MainActivity.this,lst_audio_eng[0]);
mp.start();
}
@Override
protected void onPause() {
super.onPause();
BG.pause();
}
@Override
protected void onResume() {
super.onResume();
BG.start();
}
public ViewPager.OnPageChangeListener player = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg1)
{
mp = MediaPlayer.create(MainActivity.this, lst_audio_eng[arg1]);
mp.start();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
};
}
这是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gman5541.childrensbookconcept2.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
这是我从logcat调试中得到的:
--------- beginning of crash
2018-11-25 18:40:07.511 15354-15354/com.example.gman5541.childrensbookconcept2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gman5541.childrensbookconcept2, PID: 15354
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gman5541.childrensbookconcept2/com.example.gman5541.childrensbookconcept2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at com.example.gman5541.childrensbookconcept2.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
尽管我也正在尝试使用带有“声音”开关的“播放”按钮来打开和关闭声音,但现在,我只是想将一个列表(故事板)与另一个列表(音频)进行匹配。如果我注释掉MediaPlayer行,则该程序有效。请帮忙。