我正在尝试使用configureOnClickRecyclerView方法返回recyclerView的每个元素的位置,但是在编译时,Android Studio给我一个错误:
E / Android运行时:致命异常:主要 流程:com.example.iaitmansour.myfoot,PID:1241 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.iaitmansour.myfoot / com.example.iaitmansour.myfoot.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.Object android。在空对象引用上support.v7.widget.RecyclerView.getTag(int)'
出什么问题了? (对不起,英语是我法语)
package com.example.iaitmansour.myfoot;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.example.iaitmansour.myfoot.Adapter.ExistingClubsAdapter;
import com.example.iaitmansour.myfoot.Models.ExistingClubs;
import com.example.iaitmansour.myfoot.Models.ExistingClubsList;
import com.example.iaitmansour.myfoot.Utils.GetExistingClubsDataService;
import com.example.iaitmansour.myfoot.Utils.ItemClickSupport;
import com.example.iaitmansour.myfoot.Utils.RetrofitInstance;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private ExistingClubsAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Configuring Toolbar
this.configureToolbar();
/*Create handle for the RetrofitInstance interface*/
GetExistingClubsDataService service = RetrofitInstance.getRetrofitInstance().create(GetExistingClubsDataService.class);
/*Call the method with parameter in the interface to get the employee data*/
Call<ExistingClubsList> call = service.getExistingClubsData();
/*Log the URL called*/
Log.wtf("URL Called", call.request().url() + "");
call.enqueue(new Callback<ExistingClubsList>() {
@Override
public void onResponse(Call<ExistingClubsList> call, Response<ExistingClubsList> response) {
generateExistingClubsList(response.body().getExistingClubsArrayList());
}
@Override
public void onFailure(Call<ExistingClubsList> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
this.configureOnClickRecyclerView();
}
private void configureOnClickRecyclerView(){
ItemClickSupport.addTo(recyclerView, R.layout.row_existing_clubs)
.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
Log.e("TAG", "Position : "+position);
}
});
}
/*Method to generate List of employees using RecyclerView with custom adapter*/
private void generateExistingClubsList(List<ExistingClubs> empDataList) {
recyclerView = findViewById(R.id.recycler_view_clubs_list);
adapter = new ExistingClubsAdapter(MainActivity.this, empDataList);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//2 - Inflate the menu and add it to the Toolbar
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
return true;
}
private void configureToolbar(){
// Get the toolbar view inside the activity layout
Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
// Sets the Toolbar
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle actions on menu items
switch (item.getItemId()) {
case R.id.menu_activity_main_add:
Intent addClubActivity = new Intent(MainActivity.this, AddClubActivity.class);
startActivity(addClubActivity);
default:
return super.onOptionsItemSelected(item);
}
}
}