2017-05-18 203 views
-2

我是Android應用程序開發新手。我正在創建一個簡單的音樂應用程序,它可以從用戶的外部存儲中獲取歌曲並播放它。 我爲此使用碎片。創建簡單音樂應用程序

這是我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) 

回答

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 
} 
+0

它再次 –

+0

正是同樣的原因出在同一行同樣的錯誤? '嘗試調用空對象引用虛擬方法'android.view.View android.view.View.findViewById(int)'? –

+0

是的:(。** java.lang.NullPointerException:試圖調用虛擬方法'android.view.View android.view.View.findViewById(int)'對空引用** –