我正在编写一些适用于Android的应用程序,如果在构建该项目的过程中,我出现错误:E / RecyclerView:未连接适配器;跳过布局。 应用程序可以运行,但是当我要启动此特定片段(映射片段)时,它会崩溃或再次进入主活动。但是,问题仅在于Android 9.0 Pie,而Android 8.0 Oreo则没有问题
我尝试了很多事情,但找不到根本原因,因为在要启动的片段上没有RecyclerView,因此我猜它不需要适配器。
我要开始的片段:
公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {
私有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));
}
}
这是我要转到此地图片段的活动:
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);
}
});
}
}
主要活动类(带有“设置适配器”和“设置layoutManager”)
公共类MainActivity扩展了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);
}
}
此处的崩溃日志:http://pasted.co/65607023
答案 0 :(得分:0)
移动此行:
recyclerView = findViewById(R.id.customRecyclerView);
在onCraete()
方法内
发生此错误的原因是,当调用generateDataList()
时,每次都会定义RecyclerView