当我在Android手机上调试应用程序时,我从没有收到过任何错误,但是在AVD上,我从一开始就得到了NullPointerException()。我的Android设备在Lollipop(5.1.1)上以22 API运行,我的AVD在Pie(9.0)上以28 API运行。我已将最低SDK设置为21,将编译SDK设置为28。
这是错误:
E / Android运行时:致命异常:主要 处理:com.something.something,PID:7328 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.CharSequence android.widget.TextView.getText()' 在com.something.something.FirstActivity $ 1.onEditorAction(FirstActivity.java:140)
指向FirstActivity的第140行:
String avatarInput = playerAvatar.getText()。toString();
,它引用XML属性并将其文本值分配给String。现在,我确定XML实际上具有文本值有两个原因:
1)我已经对该文本值进行了硬编码。
2)第140行在我的Android设备上未返回null。
因此,问题必须出在AVD上。我尝试了其他虚拟设备,但没有用
<TextView
android:id="@+id/avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:textSize="30sp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:singleLine="true"
android:fontFamily= "@font/font_awesome_5_pro_light_300"
android:visibility="gone"
android:text="\uf007" />
公共类FirstActivity扩展了AppCompatActivity实现的View.OnClickListener {
private static final String TAG = "FirstActivity";
private static final List<String> namesArray = new ArrayList<>();
static final String KEY_NAME = "name";
static final String KEY_AVATAR = "avatar";
static final String KEY_ICON = "icon";
FirebaseFirestore db = FirebaseFirestore.getInstance();
RelativeLayout firstActLayout;
TextView numberOfPlayers;
EditText addPlayer;
TextView doneButton;
ImageView settingsButton;
TextView sunOrMoon;
TextView playerAvatar;
TextView characterImage;
private static int playerSum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
firstActLayout = findViewById(R.id.first_act_layout);
numberOfPlayers = findViewById(R.id.number_of_players);
addPlayer = findViewById(R.id.add_player);
doneButton = findViewById(R.id.done_button);
settingsButton = findViewById(R.id.settings_button);
sunOrMoon = findViewById(R.id.sun_or_moon);
addPlayer.setOnClickListener(this);
doneButton.setOnClickListener(this);
settingsButton.setOnClickListener(this);
numberOfPlayers.setOnClickListener(this);
sunOrMoon.setOnClickListener(this);
playerAvatar = findViewById(R.id.avatar);
characterImage = findViewById(R.id.icon);
}
@Override
protected void onResume(){
super.onResume();
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.add_player:
addPlayers();
break;
case R.id.done_button:
onClickConditions();
break;
case R.id.settings_button:
break;
case R.id.number_of_players:
Toast.makeText(this, "Total number of players", Toast.LENGTH_SHORT).show();
break;
case R.id.sun_or_moon:
Toast.makeText(this, "DAY", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "DAY", Toast.LENGTH_SHORT).show();
break;
}
}
public void addPlayers (){
addPlayer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
namesArray.add(addPlayer.getText().toString());
String nameInput = addPlayer.getText().toString();
String avatarInput = playerAvatar.getText().toString();
String characterInput = characterImage.getText().toString();
addPlayer.getText().clear();
playerSum++;
numberOfPlayers.setText("");
numberOfPlayers.setText(String.valueOf(playerSum));
Map <String, Object> player = new HashMap<>();
player.put(KEY_NAME, nameInput);
player.put(KEY_AVATAR, avatarInput);
player.put(KEY_ICON, characterInput);
db.collection("Players") .document("Player_".concat(String.valueOf(playerSum)))
.set(player)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(FirstActivity.this,"Name Saved", Toast.LENGTH_SHORT)
.show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FirstActivity.this,"Error!", Toast.LENGTH_SHORT)
.show();
Log.d(TAG, e.toString());
}
});
}
return false;
}
});
}
public void onClickConditions(){
if (playerSum >= 5){
Intent intent1 = new Intent(this, SecondActivity.class);
startActivity(intent1);
return;
}
Toast.makeText(this, "Minimum of 5 players are needed", Toast.LENGTH_LONG)
.show();
}
}
我必须使用AVD的原因是因为我的应用使用了摄像头,而手机的摄像头已损坏,所以我必须在AVD上运行我的应用。我已经将我的AVD摄像机设置为我的网络摄像机,但是我必须先在第140行传递错误,然后才能在AVD上为摄像机吃午餐。
答案 0 :(得分:2)
通过互联网快速浏览,您的文字为\uf007
在这里:https://www.fileformat.info/info/unicode/char/f007/index.htm
它说
U + F007不是有效的Unicode字符。
...
Android不支持完整的Unicode字符集,它支持的部分取决于特定设备以及此设备的分发位置(实际上是运营商)。
因此,模拟器可能缺少设备所具有的语言包?因此,字符串为null
。
一种解决方案可能是查看可下载的字体:https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts
或者要证明这就是问题所在,请将文本更改为text="hello world"
:-)