我的RecyclerViews没有显示在此活动中,所以当我在手机上运行应用程序时,我查看了LogCat,只是发现了这个错误:你们中的任何人都知道它来自哪里吗?看来(根据此处的其他帖子)它可以链接到Context()
或SharedPreferences
,但我看不出它们的用途会出错:
活动
package com.olirwin.spartacus.deutsch;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class players extends AppCompatActivity {
ArrayList<String> playerNames;
Button send;
PlayerAdapter Adapter;
RecyclerView playerList;
String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_players);
playerNames = new ArrayList<String>();
final ArrayList<Player> players = new ArrayList<Player>();
send = findViewById(R.id.b_send);
Bundle extra = new Bundle();
final int numPlayers = extra.getInt("numPlayers");
System.out.println(numPlayers);
for (int i = 0; i < numPlayers; i++)
{
playerNames.add("Joueur " + (i+1) + " : ");
}
playerList = findViewById(R.id.player_list);
playerList.setLayoutManager(new LinearLayoutManager(this));
Adapter = new PlayerAdapter(playerNames,this);
playerList.setAdapter(Adapter);
names = new String[numPlayers];
Adapter.notifyDataSetChanged();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < numPlayers; i++) {
players.add(new Player(names[i]));
}
Intent intent = new Intent(view.getContext(), Score.class);
intent.putExtra("players", players);
view.getContext().startActivity(intent);
}
});
}
private class PlayerAdapter extends
RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {
private ArrayList<String> playerNames;
private Context context;
public PlayerAdapter (ArrayList<String> playerNames, Context context) {
this.playerNames = playerNames;
this.context = context;
}
@Override
public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(players.this).inflate(R.layout.list_cell, parent,
false);
return new PlayerHolder(v);
}
@Override
public void onBindViewHolder(PlayerHolder holder, final int position) {
holder.bind(this.playerNames.get(position));
holder.mPlayerName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
start, int before,
int count) {
names[position] = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int start,
int before,
int count){
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
public int getItemCount() {
return playerNames.size();
}
public class PlayerHolder extends RecyclerView.ViewHolder {
TextView mPlayerLabel;
EditText mPlayerName;
public PlayerHolder(View itemView) {
super(itemView);
mPlayerLabel = itemView.findViewById(R.id.player_label);
mPlayerName = itemView.findViewById(R.id.player_name);
}
public void bind(String playerName) {
mPlayerLabel.setText(playerName);
mPlayerName.setHint("Nom de " + playerName);
}
public String getData(){
return mPlayerName.getText().toString();
}
}
}
}
logcat的
02-28 14:13:46.476 17413-17413/? I/zygote64: Late-enabling -Xcheck:jni
02-28 14:13:46.514 17413-17449/? E/zygote64: Failed sending reply to debugger: Broken pipe
02-28 14:13:46.515 17413-17449/? I/zygote64: Debugger is no longer active
02-28 14:13:46.597 17413-17413/? D/Embryo: preload com.olirwin.spartacus.deutsch, 30ms, hwui=true, layout=false, decor=false
02-28 14:13:46.604 17413-17467/? I/Adreno: QUALCOMM build : 076f837, I801ffd6741
Build Date : 10/24/17
OpenGL ES Shader Compiler Version: EV031.20.00.04
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
02-28 14:13:46.604 17413-17467/? I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
02-28 14:13:46.601 17413-17413/? W/RenderThread: type=1400 audit(0.0:141319): avc: denied { search } for name="proc" dev="debugfs" ino=16971 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
02-28 14:13:46.609 17413-17467/? I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
02-28 14:13:46.615 17413-17467/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-28 14:13:46.615 17413-17467/? D/OpenGLRenderer: HWUI GL Pipeline
02-28 14:13:46.615 17413-17467/? D/OpenGLRenderer: Swap behavior 2
02-28 14:13:46.817 17413-17413/? E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:46.817 17413-17413/? E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:46.914 17413-17413/? D/AppTracker: App Event: start
02-28 14:13:47.012 17413-17467/? I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
02-28 14:13:49.884 17413-17413/com.olirwin.spartacus.deutsch D/AppTracker: App Event: stop
02-28 14:13:49.906 17413-17413/com.olirwin.spartacus.deutsch E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:49.906 17413-17413/com.olirwin.spartacus.deutsch E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:49.909 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: Do partial code cache collection, code=30KB, data=25KB
02-28 14:13:49.909 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: After code cache collection, code=30KB, data=25KB
02-28 14:13:49.909 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: Increasing code cache capacity to 128KB
02-28 14:13:49.917 17413-17413/com.olirwin.spartacus.deutsch E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:49.917 17413-17413/com.olirwin.spartacus.deutsch E/BoostFramework: BoostFramework() : Exception_1 = java.lang.NoSuchMethodException: perfIOPrefetchStart [int, class java.lang.String]
02-28 14:13:49.923 17413-17413/com.olirwin.spartacus.deutsch I/System.out: 0
02-28 14:13:49.927 17413-17413/com.olirwin.spartacus.deutsch D/AppTracker: App Event: start
02-28 14:13:49.990 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: Do partial code cache collection, code=37KB, data=47KB
02-28 14:13:49.990 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: After code cache collection, code=37KB, data=47KB
02-28 14:13:49.990 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: Increasing code cache capacity to 256KB
02-28 14:13:49.990 17413-17447/com.olirwin.spartacus.deutsch I/zygote64: Compiler allocated 4MB to compile void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
02-28 14:13:49.995 17413-17467/com.olirwin.spartacus.deutsch D/OpenGLRenderer: endAllActiveAnimators on 0x761eeb1400 (RippleDrawable) with handle 0x761e894f60
02-28 14:14:49.808 17413-17413/com.olirwin.spartacus.deutsch D/AppTracker: App Event: stop
答案 0 :(得分:1)
该错误与您的RecyclerView
未显示的事实无关。我希望别人知道它是什么。我建议你将适配器拆分到另一个文件中,这样就可以得到这样的结果:
<强>活动强>
public class PlayerActivity extends AppCompatActivity {
ArrayList<String> playerNames = new ArrayList<String>();
Button send;
PlayerAdapter adapter;
RecyclerView playerList;
String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_players);
final ArrayList<Player> players = new ArrayList<Player>();
send = findViewById(R.id.b_send);
Bundle extra = new Bundle();
final int numPlayers = extra.getInt("numPlayers");
System.out.println(numPlayers);
for (int i = 0; i < numPlayers; i++) {
playerNames.add("Joueur " + (i+1) + " : ");
}
playerList = findViewById(R.id.player_list);
playerList.setLayoutManager(new LinearLayoutManager(this));
adapter = new PlayerAdapter(playerNames,this);
playerList.setAdapter(Adapter);
names = new String[numPlayers];
Adapter.notifyDataSetChanged();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < numPlayers; i++) {
players.add(new Player(names[i]));
}
Intent intent = new Intent(view.getContext(), Score.class);
intent.putExtra("players", players);
view.getContext().startActivity(intent);
}
});
}
}
<强>适配器强>
public class PlayerListAdapter extends RecyclerView.Adapter<PlayerListAdapter.ViewHolder> {
private List<String> playerNames;
private Context context;
public PlayerListAdapter(ArrayList<String> playerNames, Context context) {
this.playerNames = playerNames;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(this.playerNames.get(position));
holder.playerName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(
CharSequence charSequence,
int start,
int before,
int count) {
names[position] = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int start,
int before,
int count) {}
@Override
public void afterTextChanged(Editable editable) {}
});
}
@Override
public int getItemCount() {
return playerNames.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView playerLabel;
EditText playerName;
public PlayerHolder(View view) {
super(view);
playerLabel = view.findViewById(R.id.player_label);
playerName = view.findViewById(R.id.player_name);
}
public void bind(String playerName) {
playerLabel.setText(playerName);
playerName.setHint("Nom de " + playerName);
}
public String getData(){
return playerName.getText().toString();
}
}
}
提示强>
如果RecyclerView
中的内容没有动态更改,您可以RecyclerView.setHasFixedSize(true)
来提高效果。
建议您:尝试保持一致并查看Java编码约定。 Android Studio具有自动格式化(快捷方式:Windows上的ctrl + alt + L或Mac上的选项+命令+ L),因此您可以使代码看起来更清晰。