我有MainActivity管理两个片段。从一个我选择的歌曲和另一个我正在播放它。但是当我试图运行它时,我得到了运行时异常。
片段current_song.java的代码:
package com.example.mandar.mediaplayer;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.File;
import java.util.ArrayList;
public class current_song extends Fragment {
MediaPlayer mp;
ArrayList<File> mySongs;
public current_song() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_current_song, container, false);
Intent i=getActivity().getIntent();
Bundle b=i.getExtras();
mySongs= (ArrayList) b.getParcelableArrayList("songlist");
int position = b.getInt("pos",0);
Uri u= Uri.parse(mySongs.get(position).toString());
mp= MediaPlayer.create(getActivity().getApplicationContext(),u);
mp.start();
return view;
}
}
片段songs.java的代码:
public class songs extends ListFragment {
String(String.valueOf(Environment.getExternalStorageDirectory()));
ListView lv;
String[] items;
public songs() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_songs, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
lv=getListView();
// lv = (ListView)view.findViewById(R.id.list);
final ArrayList<File> mysongs =findsongs(Environment.getExternalStorageDirectory());
items =new String[ mysongs.size()];
for (int i=0; i<mysongs.size(); i++){
//toast(mysongs.get(i).getName().toString());
items[i]=mysongs.get(i).getName().toString().replace(".mp3","");
}
ArrayAdapter<String> adp = new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.song_item,R.id.text1,items);
lv.setAdapter(adp);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getActivity().getApplicationContext(),current_song.class).putExtra("pos",position).putExtra("songlist",mysongs));
}
});
}
public void toast (String text){
Toast.makeText(getActivity().getApplicationContext(),text,Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public ArrayList<File> findsongs(File root){
ArrayList<File> a1=new ArrayList<File>();
File[] files=root.listFiles();
for (File singleFile : files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
a1.addAll(findsongs(singleFile));
}
else {
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
a1.add(singleFile);
}
}
}
return a1;
}
}
fragment_current_song.xml的代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mandar.mediaplayer.current_song">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="119dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Media Player"
android:id="@+id/textview"
android:layout_marginTop="61dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="<<"
android:width="70dp"
android:height="40dp"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="||"
android:id="@+id/button2"
android:width="70dp"
android:height="40dp"
android:layout_below="@+id/seekBar"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="70dp"
android:height="40dp"
android:id="@+id/button3"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:text=">" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:text=">>"
android:width="70dp"
android:height="40dp"
android:layout_below="@+id/seekBar"
android:layout_toRightOf="@+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView2"
android:layout_marginTop="34dp"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView3"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview" />
</RelativeLayout>
</FrameLayout>
fragment_songs.xml的代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mandar.mediaplayer.songs">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="440dp"
android:id="@android:id/list"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
错误日志:
04-17 22:20:34.035 26799-26799/com.example.mandar.mediaplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mandar.mediaplayer, PID: 26799
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference
at com.example.mandar.mediaplayer.current_song.onCreateView(current_song.java:36)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:199)
at android.view.View.measure(View.java:17637)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
at android.view.View.measure(View.java:17637)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
答案 0 :(得分:0)
用于启动活动的Intent不包含任何额外内容,因此getExtras()
会返回null
。您应该编写代码以相应地处理此类情况,或者如果这不是Activity的有效状态,则自己抛出异常(在这种情况下,您需要确保只使用具有适当附加功能的Intent启动Activity)