问题描述:

 FATAL EXCEPTION: main

java.lang.NullPointerException

at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1545)

at android.view.View.onDrawScrollBars(View.java:11567)

at android.view.View.draw(View.java:13931)

at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3037)

at android.view.View.getDisplayList(View.java:12832)

at android.view.View.getDisplayList(View.java:12876)

at android.view.View.draw(View.java:13621)

at android.view.ViewGroup.drawChild(ViewGroup.java:3037)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2901)

at android.view.View.getDisplayList(View.java:12827)

at android.view.View.getDisplayList(View.java:12876)

at android.view.View.draw(View.java:13621)

at android.view.ViewGroup.drawChild(ViewGroup.java:3037)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2901)

at android.view.View.draw(View.java:13928)

at android.widget.FrameLayout.draw(FrameLayout.java:467)

at android.view.View.getDisplayList(View.java:12832)

at android.view.View.getDisplayList(View.java:12876)

at android.view.View.draw(View.java:13621)

at android.view.ViewGroup.drawChild(ViewGroup.java:3037)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2901)

at android.view.View.getDisplayList(View.java:12827)

at android.view.View.getDisplayList(View.java:12876)

at android.view.View.draw(View.java:13621)

at android.view.ViewGroup.drawChild(ViewGroup.java:3037)

at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2901)

at android.view.View.getDisplayList(View.java:12827)

this is my Code

public class testclass extends AppCompatActivity {

ArrayList<FlightAvilablityModel> flightAvilablityModelArrayList;

SoapSerializationEnvelope envelope;

private RecyclerView recyclerView;

JSONArray _jarArray;

JSONObject _JsonObject;

private FlightAvilablityAdapter mAdapter;

RecyclerView.LayoutManager mLayoutManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

new flightAvilblity().execute();

}

class flightAvilblity extends AsyncTask<Void, Void, Void> {

@Override

protected Void doInBackground(Void... params) {

try {

SoapObject request = new SoapObject(Constant.NAME_SPACE, Constant.GET_FLIGHT_AVILABLITY_JSON);

request.addProperty("Origin", "del");

request.addProperty("Destination", "pat");

request.addProperty("frmdate", "2017-03-03");

request.addProperty("todate", "");

request.addProperty("adl", "1");

request.addProperty("chd", "0");

request.addProperty("inf", "0");

request.addProperty("travelclass", "E");

request.addProperty("travelmodeway", "one");

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;

envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(Constant.URL);

androidHttpTransport.debug = true;

androidHttpTransport.call(Constant.NAME_SPACE + "/" + Constant.GET_FLIGHT_AVILABLITY_JSON, envelope);

String result = ((SoapObject) envelope.bodyIn).getProperty(0).toString();

_jarArray = new JSONArray(result);

flightAvilablityModelArrayList = new ArrayList<>();

for (int i = 0; i < _jarArray.length(); i++) {

FlightAvilablityModel avilablityModel = new FlightAvilablityModel();

_JsonObject = _jarArray.getJSONObject(i);

avilablityModel.setFlightsegment_id(_JsonObject.getString("Flightsegment_id"));

avilablityModel.setImagefileName(_JsonObject.getString("ImagefileName"));

avilablityModel.setActualbasefare(_JsonObject.getString("actualbasefare"));

avilablityModel.setTax(_JsonObject.getString("tax"));

avilablityModel.setArrivalAirportCode(_JsonObject.getString("ArrivalAirportCode"));

avilablityModel.setArrivalDateTime(_JsonObject.getString("ArrivalDateTime"));

avilablityModel.setDepartureAirportCode(_JsonObject.getString("DepartureAirportCode"));

avilablityModel.setDepartureDateTime(_JsonObject.getString("DepartureDateTime"));

avilablityModel.setFlightNumber(_JsonObject.getString("FlightNumber"));

avilablityModel.setAdultFare(_JsonObject.getString("adultFare"));

avilablityModel.setClassType(_JsonObject.getString("timediff"));

flightAvilablityModelArrayList.add(avilablityModel);

}

Log.d("Response", result.toString());

} catch (SoapFault e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (XmlPullParserException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NullPointerException e) {

// TODO: handle exception

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(Void aVoid) {

super.onPostExecute(aVoid);

mAdapter = new FlightAvilablityAdapter(flightAvilablityModelArrayList);

LinearLayoutManager verticalLayoutmanager

= new LinearLayoutManager(testclass.this, LinearLayoutManager.VERTICAL, false);

recyclerView.setLayoutManager(verticalLayoutmanager);

recyclerView.setAdapter(mAdapter);

mAdapter.notifyDataSetChanged();

}

}

}

I am getting data in flightAvilablityModelArrayList described in testclass, but when I bind data in onpost Execute method, then I am getting that Exception. I don't know why this Error is coming.

Please suggest me where am I doing wrong!

网友答案:

Set scrollbars as vertical in RecyclerView

android:scrollbars="vertical"

In your gradle check once you added following dependencies

dependencies {   
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:22.0.0'    
}
相关阅读:
Top