혼자 책의 예제를 엑티비티 방식에서 프레그먼트 방식으로 변경해서 테스트를 해보려는데 계속
에러가 발생해서 질문을 드립니다. 아래는 소스 내용입니다.
package com.example.test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class Fragment2 extends Fragment {
RequestQueue requestQueue;
RecyclerView recyclerView;
MovieAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
makeRequest(rootView);
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(getActivity());
}
recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
return rootView;
}
private void makeRequest(View rootView) {
String url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=24258683e583fa09cb007d36fd084860&targetDt=20191101";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
println("응답 -> " + response);
processResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
println("에러 -> " + error.getMessage());
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String,String>();
return params;
}
};
request.setShouldCache(false);
requestQueue.add(request);
println("요청 보냄.");
}
public void println(String data) {
Log.d("Fragment", data);
}
public void processResponse(String response) {
Gson gson = new Gson();
MovieList movieList = gson.fromJson(response, MovieList.class);
println("영화정보의 수 : " + movieList.boxOfficeResult.dailyBoxOfficeList.size());
for (int i = 0; i < movieList.boxOfficeResult.dailyBoxOfficeList.size(); i++) {
Movie movie = movieList.boxOfficeResult.dailyBoxOfficeList.get(i);
adapter.addItem(movie);
}
adapter.notifyDataSetChanged();
}
}
아래의 부분에서 계속 에러가 발생을 하는데 이유를 잘 모르겠네요..
makeRequest(rootView);
requestQueue.add(request);
아래는 에러 코드입니다.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 4675
java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference
at com.example.test.Fragment2.makeRequest(Fragment2.java:75)
at com.example.test.Fragment2.onCreateView(Fragment2.java:33)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)