私は、オブジェクトのリストからリサイクラービューのリストを表示するフラグメントを持っています。私のフラグメントのクラスは、次の通りである:私のupdateUI()
で.size()メソッドでNullPointerExceptionが発生しましたか?
// A simple {@link Fragment} subclass.
public class RedditListFragment extends Fragment implements AsyncResponse {
AsyncResponse asyncResponse;
private RecyclerView mRecyclerView;
private RedditAdapter mAdapter;;
private List<RedditObject> objectList;
Parser parse;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
executeAsyncTask();
View view = inflater.inflate(R.layout.fragment_reddit_list, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.reddit_list_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
// Inflate the layout for this fragment
return view;
}
public void executeAsyncTask(){
DownLoadRawData downLoadRawData = new DownLoadRawData();
downLoadRawData.delegate = this;
downLoadRawData.execute("https://www.reddit.com/r/Showerthoughts/.json");
// Log.d("ERR", " TEXT WAS " + jsonTEXT);
}
@Override
public void processFinish(String output) {
Log.d("TAG", "What is happening!");
Log.d("TAG", "process finished reached");
parse = new Parser(output);
this.objectList = parse.getRedditObjectsList();
Log.d("TAG", "OBJECT LIST WAS" + objectList.toString());
Log.d("TAG", "The List to STring is " + parse.getRedditObjectsList().toString());
}
public void updateUI(){
List<RedditObject> listOfRedditObject = objectList;
mAdapter = new RedditAdapter(listOfRedditObject);
mRecyclerView.setAdapter(mAdapter);
}
private class RedditHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public RedditHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView; //by casting itemView to a TextView, this forces client to pass text view
}
}
private class RedditAdapter extends RecyclerView.Adapter<RedditHolder>{
private List<RedditObject> mListOfRedditObjects;
public RedditAdapter(List<RedditObject> listOfRedditObject){
mListOfRedditObjects = listOfRedditObject;
}
@Override
public RedditHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.single_reddit_listing, parent, false);
return new RedditHolder(view);
}
@Override
public void onBindViewHolder(RedditHolder holder, int position) {
RedditObject redditObject = mListOfRedditObjects.get(position);
holder.mTextView.setText(redditObject.getmTitle());
}
@Override
public int getItemCount() {
// Log.d("TAG ", "In the adapter and can confirm" + mListOfRedditObjects.size());
return mListOfRedditObjects.size();
}
}
}
、私はobjectListリスト<>がnullでないことを確認しました。しかし、何らかの理由で、AndroidがgetItemsCount()で配列のサイズを取得するコードの一番下にあります。私はいつもこのエラーを受け取ります:
12-15 21:00:11.532 26097-26097/com.example.ridhwaan.redditforfaez E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ridhwaan.redditforfaez, PID: 26097
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.ridhwaan.redditforfaez.RedditListFragment$RedditAdapter$override.getItemCount(RedditListFragment.java:144)
at com.example.ridhwaan.redditforfaez.RedditListFragment$RedditAdapter$override.access$dispatch(RedditListFragment.java)
at com.example.ridhwaan.redditforfaez.RedditListFragment$RedditAdapter.getItemCount(RedditListFragment.java:0)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3289)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3186)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
at android.view.View.layout(View.java:17522)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17522)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17522)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17522)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17522)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at (more exception lines)
私はこれを2日間ログステートメントとデバッグを使って修正しようとしました。詳しい情報が必要な場合は、私はあなたにそれを与えることができます。ここで
は、非同期タスクのクラスです。この中
public class DownLoadRawData extends AsyncTask<String, Void, String> {
private String mFileContents;
public AsyncResponse delegate = null;
public String getmFileContents() {
return mFileContents;
}
@Override
protected String doInBackground(String... strings) {
mFileContents = downloadRawJSON(strings[0]);
return mFileContents;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
delegate.processFinish(result);
Log.d("TAG", "RESULT" + result);
}
private String downloadRawJSON(String urlSpec){
Log.d("TAG", "DOWNLOADING" + " " + urlSpec);
StringBuilder sb = new StringBuilder();
try{
Log.d("TAG", "ENTETRED TRY BLOCK");
URL url = new URL(urlSpec);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int response = conn.getResponseCode();
Log.d("TAG" , "RESPONSE CODE" + " " + response);
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int charRead;
char[] buffer = new char[500];
while(true){
charRead = isr.read(buffer); // returns number read + reads into parameter
if(charRead <= 0){
Log.d("TAG","BROKE");
break;
}
sb.append(String.copyValueOf(buffer,0,charRead));
}
return sb.toString();
}catch (Exception e){
Log.d("TAG", "FATAL ERROR" + " BECASE " + e.getMessage());
e.printStackTrace();
}
Log.d("TAG" , " ERROR NO DATA");
return null;
}
}
、processFinish
は、コールバックメソッドです。
を変更する必要があります"objectList"が "DownLoadRawData" Asynctaskに設定されていますか? DownLoadRawDataのonPostExecuteでupdateUI呼び出しを移動します。それはうまくいけばそれを修正するでしょう。それ以外の場合は、asynctaskのコードも投稿してください。 – Hahn
AsyncTaskを追加しました。 – Ridhwaan
onCreateViewからupdateUiを削除します。次に、processFinishコールバックの最後に、updateUiを呼び出します。 – Hahn