2014-11-25 45 views
-2

我下面有下面的代碼。它在Activity中工作。現在我想將它轉換爲Fragment。我試過context.getContentResolver();getActivity()。getContentResolver();但它不起作用。如何解決這個問題?java.lang.NullPointerException是由片段中的getContentResolver()引起的

public class MyFragmentA extends Fragment { 

//song list variables 
    private ArrayList<Song> songList; 
    private ListView songView; 
    public Context context; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false); 

    //retrieve list view 
      songView = (ListView) myFragmentView.findViewById(R.id.song_list); 
      //instantiate list 
      songList = new ArrayList<Song>(); 
      //get songs from device 
      getSongList(); 
      //sort alphabetically by title 
      Collections.sort(songList, new Comparator<Song>(){ 
       public int compare(Song a, Song b){ 
        return a.getTitle().compareTo(b.getTitle()); 
       } 
      }); 
      //create and set adapter 
      SongAdapter songAdt = new SongAdapter(this.getActivity(), songList); 
      songView.setAdapter(songAdt); 

    return myFragmentView; 
} 


public void getSongList() { 

ContentResolver musicResolver = context.getContentResolver(); 
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 
---------------------- 
---------------------- 
---------------------- 
---------------------- 
      } 
     } 
    } 

這是我的logcat跟蹤

11-25 20:29:34.001: E/AndroidRuntime(4092): FATAL EXCEPTION: main 
11-25 20:29:34.001: E/AndroidRuntime(4092): Process: com.ddev.delta, PID: 4092 
11-25 20:29:34.001: E/AndroidRuntime(4092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddev.delta/com.ddev.delta.AndroidNavigationTabsActivity}: java.lang.NullPointerException 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread.access$800(ActivityThread.java:144) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.os.Looper.loop(Looper.java:136) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread.main(ActivityThread.java:5146) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at dalvik.system.NativeStart.main(Native Method) 
11-25 20:29:34.001: E/AndroidRuntime(4092): Caused by: java.lang.NullPointerException 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at com.ddev.delta.AndroidNavigationTabsActivity.onCreate(AndroidNavigationTabsActivity.java:17) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.Activity.performCreate(Activity.java:5231) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
11-25 20:29:34.001: E/AndroidRuntime(4092):  ... 12 more 
+0

哪個代碼中的第57行? '57號線發生異常。 – 2014-11-25 13:07:59

+2

上下文爲空...使用getActivity()。getContentResolver(); – 2014-11-25 13:08:39

+0

'AndroidNavigationTabsActivity.onCreate(AndroidNavigationTabsActivity.java:17)'中有另一個NPE' – laalto 2014-11-25 13:37:17

回答

6
ContentResolver musicResolver = context.getContentResolver(); 

context未初始化。

既然你在一個片段生命週期回調onCreateView()或方法,由一個叫的時候,你可以使用getActivity()Context

ContentResolver musicResolver = getActivity().getContentResolver(); 

取出Context context;聲明不必要的了。

+0

這不起作用。 – vallen 2014-11-25 13:18:57

+0

隨意解釋什麼「不起作用」對你意味着什麼。編輯問題來完成。 – laalto 2014-11-25 13:27:41

+0

我的錯誤仍然相同java.lang.NullPointerException – vallen 2014-11-25 13:30:29

0
public Context context; 

你的背景是,儘量使用getActivity().getContentResolver();

+0

它不起作用。 – vallen 2014-11-25 13:20:08

0

在onCreateView()初始化上下文對象之前調用getSongList():

context = getActivity(); 

OR

getActivity().getContentResolver() 
+0

它不起作用。 – vallen 2014-11-25 13:19:46

+0

@ vallen,仍然給出了任何錯誤? – 2014-11-25 13:20:44

+0

錯誤仍然相同。 java.lang.NullPointerException – vallen 2014-11-25 13:23:11

0

創建E活性的一個對象,然後通過

Activity a=getActivity(); 

初始化,然後使用

ContentResolver musicResolver = a.getContentResolver(); 
0

使用以下這些情況:

  • 應用: - getApplicationContext()
  • 活動: - 背景
  • 片段: - getActivity()
相關問題