我是Android App Development的新手。我正在创建一个简单的音乐应用程序,从用户的外部存储中获取歌曲并播放它。 我正在使用Fragments。
这是我的SongList.class代码============================
package com.android.jarvis.easysplay;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Devil on 5/17/2017.
*/
public class SongsList extends android.support.v4.app.Fragment {
ListView lv;
String items;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
lv =(ListView) getView().findViewById(R.id.lvPlaylist);
ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
for(int i=0; i<mySongs.size(); i++){
toast(mySongs.get(i).getName().toString());
}
return inflater.inflate(R.layout.songs_list, container, false);
}
public ArrayList<File> findSongs(File root){
ArrayList<File> al = new ArrayList<>();
File[] files = root.listFiles();
for(File singleFile:files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
al.addAll(findSongs(singleFile));
}else {
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
al.add(singleFile);
}
}
}
return al;
}
public void toast(String text){
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
}
当我开始此活动时,它显示以下错误。我不知道该怎么做
05-18 21:58:56.747 18725-18725/com.android.jarvis.easysplay E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.jarvis.easysplay, PID: 18725
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.android.jarvis.easysplay.SongsList.onCreateView(SongsList.java:32)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:3)
您尝试在充分展开之前访问您的观点,因此它尚不存在(因此NullPointerException
)。将您的onCreateView()
方法更改为以下内容:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.songs_list, container, false);
lv =(ListView) view.findViewById(R.id.lvPlaylist);
ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
for(int i=0; i<mySongs.size(); i++){
toast(mySongs.get(i).getName().toString());
}
return view
}