试图在其中找到空指针。直到我对列表实现了on click侦听器之前,它都运行良好。 Android开发的新手,谢谢您的评论。
这是主要活动
import arcpy
#First check to see if the covered_routes field exists yet
if len(arcpy.ListFields(Metro_Stops,"covered_routes")) > 0:
arcpy.DeleteField_management(Metro_Stops_Buffer, "covered_routes")
arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string")
else:
arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string")
#Delete Extra Layers
arcpy.Delete_management('Metro_Routes_lyr')
arcpy.Delete_management('Metro_Stops_Buffer_lyr')
#A Cursor that runs through each Stop.
with arcpy.da.UpdateCursor(Metro_Stops_Buffer, ("covered_routes","SHAPE@")) as cursor:
for row in cursor:
routes = []
clean_row = '{}'.format(row[0])
#Create the layers to work with from the GDB.
arcpy.MakeFeatureLayer_management(Metro_Stops_Buffer, 'Metro_Stops_Buffer_lyr')
arcpy.MakeFeatureLayer_management(Metro_Routes, 'Metro_Routes_lyr')
#Select the routes that intersect the stop (row)
arcpy.SelectLayerByLocation_management('Metro_Routes_lyr', "INTERSECT", row[1])
#Given the selected routes, create a new cursor that will return the route name (route_long) for each.
with arcpy.da.SearchCursor('Metro_Routes_lyr', "route_long") as cursor2:
for j in cursor2:
clean_j = '{}'.format(j[0])
print(clean_j)
routes.append(clean_j)
print(routes)
#Insert the routes into the "covered_routes" field.
row[0] = str(routes)
cursor.updateRow(row)
del(routes)
#Remove the unnecessary layers
arcpy.Delete_management('Metro_Routes_lyr')
arcpy.Delete_management('Metro_Stops_Buffer_lyr')
这是它的适配器:
public class CoachActivity extends AppCompatActivity {
EditText sv;
private ArrayList<Swimmer> mSwimmers;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master_coach);
getSwimmers();
buildRecyclerView();
EditText editText = findViewById(R.id.mSearch);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
}
private void filter(String text) {
ArrayList<Swimmer> filteredList = new ArrayList<>();
for (Swimmer item : mSwimmers) {
if (item.getmSwimmerName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
//ADD PLAYERS TO ARRAYLIST
private void getSwimmers()
{
mSwimmers = new ArrayList<>();
mSwimmers.add(new Swimmer("Johnny"));
mSwimmers.add(new Swimmer("Trinity"));
mSwimmers.add(new Swimmer("NEO"));
}
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.myRecycler);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
//mAdapter = new MyAdapter(mSwimmers);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setAdapter(new MyAdapter(mSwimmers, new MyAdapter.ItemClickListener() {
@Override public void onItemClick(Swimmer swimmer) {
Toast.makeText(getBaseContext(), "Item Clicked", Toast.LENGTH_LONG).show();
// call the progression fragment
Fragment fragment = ProgressionFragment.newInstance();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.fragment_container_detail, fragment)
.commit();
}
}));
}
}
当我添加OnClickListener来启动片段时,是空指针发生的时间。这是错误:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ExampleViewHolder> {
public interface ItemClickListener {
void onItemClick(Swimmer item);
}
private ArrayList<Swimmer> swimmers;
private final ItemClickListener listener;
public MyAdapter(ArrayList<Swimmer> swimmers, ItemClickListener listener) {
this.swimmers = swimmers;
this.listener = listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView1;
public ExampleViewHolder(View itemView) {
super(itemView);
mTextView1 = itemView.findViewById(R.id.nameTxt);
}
public void bind(final Swimmer swimmer, final ItemClickListener listener) {
mTextView1.setText(swimmer.getmSwimmerName());
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(swimmer);
}
});
}
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,
parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
Swimmer currentItem = swimmers.get(position);
holder.mTextView1.setText(currentItem.getmSwimmerName());
holder.bind(swimmers.get(position), listener);
}
@Override
public int getItemCount() {
return swimmers.size();
}
public void filterList(ArrayList<Swimmer> filteredList) {
swimmers = filteredList;
notifyDataSetChanged();
}
}