我的代码有问题。实际上,这是玩家要输入的名称。它使用类“玩家输入”,它引用类“ nameListAdapter”。这似乎是在撒谎,但很遗憾,我没有看到问题所在
一切正常。然后,我尝试使用“可序列化”进行操作。那不起作用,当我回到原始代码时,它不再起作用。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.matthias.startanimation.spieler.Spieler;
import java.util.List;
public class Spielereingabe extends Activity {
List<Spieler> spielerList;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spielereingabe);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
//Button
button = (Button) findViewById(R.id.zukategorieButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openKategorien_n();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
RecyclerView recyclerView = findViewById(R.id.recycler_namensListe);
final NamensListeAdapter namensListeAdapter = new NamensListeAdapter(this, spielerList);
recyclerView.setLayoutManager(new LinearLayoutManager(Spielereingabe.this));
recyclerView.setAdapter(namensListeAdapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
namensListeAdapter.removeItem(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
// view the background view
}
};
// attaching the touch helper to recycler view
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
}
//mathode für button
public void openKategorien_n() {
Intent intent = new Intent(this, Kategorien_n.class);
startActivity(intent);
}
//pop-up Dialog
private void showDialog() {
View ppview = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popup_enter_name, null);
final EditText editText = ppview.findViewById(R.id.name_edit);
final RadioGroup radioGroup = ppview.findViewById(R.id.name_radiogroup);
final AlertDialog.Builder builder = new AlertDialog.Builder(Spielereingabe.this);
builder.setView(ppview)
.setTitle("Namen Eingeben")
.setPositiveButton("Speicher", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (!editText.getText().toString().isEmpty())
spielerList.add(new Spieler()
.setName(editText.getText().toString())
.setGeschlecht(radioGroup.getCheckedRadioButtonId() == R.id.radio_male ? Gender.MALE : Gender.FEMALE));
Toast.makeText(Spielereingabe.this, editText.getText().toString() + " wurde hinzugefügt", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.show();
}
}
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.matthias.startanimation.spieler.Spieler;
import java.util.List;
public class NamensListeAdapter extends RecyclerView.Adapter<NamensListeAdapter.NameViewHolder> {
private List<Spieler> spielerList;
private LayoutInflater inflater;
public NamensListeAdapter(Context context, List<Spieler> namensList) {
this.spielerList = namensList;
this.inflater = LayoutInflater.from(context);
}
@Override
public NameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NameViewHolder(inflater.inflate(R.layout.name_recyclerview, parent, false));
}
@Override
public void onBindViewHolder(NameViewHolder holder, int position) {
Spieler spieler = spielerList.get(position);
String text = spieler.getName() + " (" + spieler.getGeschlecht().getCaption() + ")";
holder.textView_name.setText(text);
}
@Override
public int getItemCount() {
return spielerList.size();
}
public void removeItem(int itemIndex) {
spielerList.remove(itemIndex);
notifyItemRemoved(itemIndex);
}
public class NameViewHolder extends RecyclerView.ViewHolder {
TextView textView_name;
public NameViewHolder(View itemView) {
super(itemView);
textView_name = itemView.findViewById(R.id.nameview_text);
}
}
}
错误消息
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.matthias.startanimation.NamensListeAdapter.getItemCount(NamensListeAdapter.java:39)
看到任何人的错误吗?
答案 0 :(得分:0)
spielerList
尚未初始化。在Spielereingabe.java
类中,初始化列表:
private final List<Spieler> spielerList = new Arraylist<>();
最好在NamensListeAdapter.class
中做同样的事情,但是在这种情况下,它不会是final
变量。