0
私はMainActivityを二つのフラグメントを管理しています。一つは私が選んだ曲、もう一つは演奏しています。しかし、私はそれを実行しようとしているときに私はruntimeExceptionを取得しています。フラグメントcurrent_song.javaためヌルオブジェクトリファレンスで仮想メソッド 'Bundle.getParcelableArrayList(java.lang.String)'を呼び出そうとしています
コード:フラグメントsongs.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;
}
}
コード: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.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_current_song.xmlため
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;
}
}
コード
<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)
よろしくお願いいたします。しかし、私は2つの断片を切り替える意思を使用するとき、コードは正しいですか?そうでない場合、私はそれについて行き、すべての(エクストラ)をある断片から別の断片に渡すのですか? – maddy
@maddy値を読み取ろうとする前に、バンドルがヌルでないことをチェックする方が良いです( 'b!= null')。残りのコードがどのように機能するのか分かりません。他の問題に遭遇した場合、これを複合するのではなく新しい質問をする必要があります。 – Karakuri
大丈夫です..... – maddy