我有一个带有RecyclerView(trackingsRecycler)的MainActivity和一个列出CardView的适配器,它可以正常工作。
当我点击某个cardview时,应用程序调用(使用Intent)另一个活动(DetailsActivity),它应该列出另一个RecyclerView(detailsRecycler)。
但在DetailsActivity中的代码
RecyclerView detailsRecyclerView = findViewById(R.id.recycler_details_view);
不起作用
findViewById返回'null'
任何人都可以帮助我吗?
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String TRACKING = "com.example.cooltracker.MESSAGE";
private RecyclerView recyclerView;
private TrackingsAdapter adapter;
private List<Tracking> trackingList;
private ProgressDialog pDialog;
private String webserviceURL;
private TextView total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webserviceURL = "http://myWebServiceUrl.com/params";
---> recyclerView = findViewById(R.id.recycler_main_view); <--- Ok!!
trackingList = new ArrayList<>();
adapter = new TrackingsAdapter(this, trackingList);
total = findViewById(R.id.tvTotal);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(5), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
...
public class TrackingsAdapter extends RecyclerView.Adapter<TrackingsAdapter.MyViewHolder> {
private Context mContext;
private List<Tracking> trackingList;
public class MyViewHolder extends RecyclerView.ViewHolder {...}
@override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tracking_card, parente, attachToRoot:false);
final MyViewHolder mViewHolder = new MyViewHolder(itemView);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder...){
...
...
...
//Here I set the click listener
holder.card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDetails(tracking.getTrack());
}
});
private void getDetails(String tracking){
//Call next screen DetailsActivity...
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.putExtra(TRACKING, tracking);
mContext.startActivity(intent);
}
到目前为止,它运作良好!代码调用下一个屏幕......
但是...
DetailsActivity.java
public class DetailsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DetailsAdapter adapter;
private List<Tracking> detailList;
private ProgressDialog pDialog;
private String webserviceURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
//get intent
Intent intent = getIntent();
String tracking = intent.getStringExtra(MainActivity.TRACKING);
//set string in layout
TextView tvHeaderLine1 = findViewById(R.id.tvHeaderLine1);
tvHeaderLine1.setText(tracking);
webserviceURL = "http://werService.com/otherAction";
//THIS PART IS NOT WORKING ///
----> recyclerView = findViewById(R.id.recycler_detail_view); <----
System.out.println(recyclerView); // It's always returning NULL
detailList = new ArrayList<>();
adapter = new DetailsAdapter(this, detailList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
//recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.setAdapter(adapter);
// prepare the loading Dialog
pDialog = new ProgressDialog(this);
pDialog.setMessage("Message");
pDialog.setCancelable(false);
...
...
}
}
答案 0 :(得分:0)
解决!!!
首先我想......如果我使用回收商,为什么我需要两个?我应该使用相同的...所以我设置
setContentView(R.layout.activity_main)
和我使用相同的...谢谢!!!
我现在正在改进设计......
屏幕在这里......我需要至少10个声望才能发布图片... = /
谢谢!