Issue
I am trying to populate a Spinner from a JSON response using an async task. I am getting the JSON object as a server response and the response is correct. Here it is.
[{"category_id":1,"category_name":"History"},{"category_id":2,"category_name":"Sports"},{"category_id":3,"category_name":"Science"},{"category_id":4,"category_name":"gg"}]
I am calling an asynctask on the View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstranceState)
But I am getting a 'null object reference error' on HomeFragment activity launch.
Here is my SpinnerAdapter.class.
package com.example.quizapp;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.example.quizapp.model.Category;
import java.util.List;
public class SpinnerAdapter extends BaseAdapter
{
private LayoutInflater layoutInflater;
private List<Category> categoryList;
private Context context;
public SpinnerAdapter(Context context, List<Category> list){
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.categoryList = list;
}
@Override
public int getCount() {
return categoryList.size();
}
@Override
public Object getItem(int position) {
return (Category)categoryList.get(position);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder spinnerHolder;
if(convertView == null){
spinnerHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.category_spinner_text_layout, parent,false);
spinnerHolder.spinnerItemList = (TextView) convertView.findViewById(R.id.catsSpinnerText);
convertView.setTag(spinnerHolder);
}else {
spinnerHolder = (ViewHolder) convertView.getTag();
}
spinnerHolder.spinnerItemList.setText(categoryList.get(position).getCatName());
return convertView;
}
class ViewHolder{
TextView spinnerItemList;
}
}
This is the AsyncTask for populating the Spinner written in the HomeFragment.class.
public class CategoryLoadAsyncTask extends AsyncTask<Void, String, Category[]> {
@Override
protected void onPreExecute(){
progressDialog = ProgressDialog.show(getActivity(), "Loading Game", "Please wait..");
}
@Override
protected Category[] doInBackground(Void... params) {
String json ="";
Category[] categories = null;
try {
json = new RestClient().doGet(getString(R.string.category_url));
categories = new Gson().fromJson(json, Category[].class);
} catch (Exception e) {
e.printStackTrace();
}
return categories;
}
@Override
protected void onPostExecute(Category[] categories){
progressDialog.dismiss();
if (categories != null){
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(context, Arrays.asList(categories));
catsSpinner.setAdapter(spinnerAdapter);
}
}
}
Here is the error log.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quizapp, PID: 2381
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:90)
at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:64)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Can anybody help me with this?
Solution
It looks like that you forgot the initialize catsSpinner variable. Can you share the related part from your HomeFragment or can you check your code? As you know, the best way to find views in your fragment is onViewCreated callback. Let me know when you update question.
Answered By - Metehan Toksoy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.