0

I'm writing some application for Android and during the building if the project, I have an error: E/RecyclerView: No adapter attached; skipping layout. Application works but when I want to start this particular fragment (maps fragment) it crashes or goes again to the main activity. However the problem is only with Android 9.0 Pie, no problem with Android 8.0 Oreo

I tried many things but I couldn't find the root cause because on the fragment I want to start there is no RecyclerView thus I guess it doesn't need an adapter.

Fragment that I want to start:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    GPSTracker tracker = new GPSTracker(MapsActivity.this);
    double lat=tracker.getLatitude();
    double lng=tracker.getLongitude();

    // Add a marker for current location and move the camera
    LatLng currentLocation = new LatLng(lat, lng);
    mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location Marker"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
  }
}

And this is the activity from which I want to go to this Maps Fragment:

public class DescriptionActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_description);
    Bundle bundle = getIntent().getExtras();
    int id = bundle.getInt("ID");
    String detailedDescription = bundle.getString("TITLE");
    String imageUrl = bundle.getString("IMAGE_URL");
    Button navigate = (Button) findViewById(R.id.navigate);
    TextView idTxt = (TextView) findViewById(R.id.detailsText);
    TextView detailedDescTxt = (TextView) findViewById(R.id.detailedDescription);
    ImageView dishImage = (ImageView) findViewById(R.id.dishImage);
    idTxt.setText("ID " + String.valueOf(id));
    detailedDescTxt.setText(detailedDescription);
    Picasso.get().load(imageUrl).into(dishImage);

    navigate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), MapsActivity.class);
            v.getContext().startActivity(intent);
        }
    });
  }
}

Main Activity class (with "set adapter" and "set layoutManager")

public class MainActivity extends AppCompatActivity {

private RecyclerListAdapter adapter;
private RecyclerView recyclerView;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Loading....");
    progressDialog.show();

    /*Create handle for the RetrofitInstance interface*/
    GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
    Call<List<RetroList>> call = service.getAllPhotos();
    call.enqueue(new Callback<List<RetroList>>() {
        @Override
        public void onResponse(Call<List<RetroList>> call, Response<List<RetroList>> response) {
            progressDialog.dismiss();
            generateDataList(response.body());
        }

        @Override
        public void onFailure(Call<List<RetroList>> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}

/*Method to generate List of data using RecyclerView with custom adapter*/
private void generateDataList(List<RetroList> photoList) {
    recyclerView = findViewById(R.id.customRecyclerView);
    adapter = new RecyclerListAdapter(this, photoList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

}

Crash log here: http://pasted.co/65607023

JorgulMaster
  • 1
  • 1
  • 3

1 Answers1

0

move this line :

recyclerView = findViewById(R.id.customRecyclerView);

inside onCraete() Methode

This Error Happens because when called generateDataList() will define the RecyclerView everytime

Ahmad Darwesh
  • 553
  • 4
  • 12