简体   繁体   English

Android应用程式可在手机上运作,但无法在AVD上运作

[英]Android app works on phone but not on AVD

When I debug the app on my android phone I get absolutely no errors what so ever, but on AVD I get a NullPointerException() right from the get-go. 当我在Android手机上调试应用程序时,我从没有收到过任何错误,但是在AVD上,我从一开始就得到了NullPointerException()。 My Android Device is running on Lollipop (5.1.1) at 22 API, my AVD is running on Pie (9.0) at 28 API. 我的Android设备在Lollipop(5.1.1)上以22 API运行,我的AVD在Pie(9.0)上以28 API运行。 I have set the minimum SDK to 21 and the compile SDK to 28 in my gradle. 我已将最低SDK设置为21,将编译SDK设置为28。

This is the error: 这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.something.something, PID: 7328 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.something.something.FirstActivity$1.onEditorAction(FirstActivity.java:140) E / AndroidRuntime:致命例外:主进程:com.something.something,PID:7328 java.lang.NullPointerException:尝试在空对象上调用虚拟方法'java.lang.CharSequence android.widget.TextView.getText()'参考com.something.something.FirstActivity $ 1.onEditorAction(FirstActivity.java:140)

Which points to line 140 of the FirstActivity: 指向FirstActivity的第140行:

String avatarInput = playerAvatar.getText().toString(); 字符串avatarInput = playerAvatar.getText()。toString();

which is referencing an XML attribute and assigning its text value to a String. 引用XML属性并将其文本值分配给String。 Now I am sure that the XML has in fact a text value for 2 reasons: 现在,我确定XML实际上具有文本值有两个原因:

1) I have hard-coded that text value myself. 1)我已经硬编码了该文本值。

2) line 140 does not return null on my Android Device. 2)第140行在我的Android设备上不返回null。

The problem therefore must be from the AVD. 因此,问题必须出在AVD上。 I have tried other virtual devices but none work 我尝试了其他虚拟设备,但无济于事

<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" />

public class FirstActivity extends AppCompatActivity implements View.OnClickListener { 公共类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();
}

} }

The reason why I have to use an AVD is because my app uses the camera and my phone's camera is busted so I have to run my app on the AVD. 之所以必须使用AVD,是因为我的应用使用了摄像头,而手机的摄像头已损坏,因此我必须在AVD上运行我的应用。 I have already set my AVD camera to my webcam, but I have to move passed the error on line 140 before I can lunch the camera on AVD. 我已经将我的AVD摄像机设置为我的网络摄像机,但是我必须先在第140行传递错误,然后才能在AVD上为摄像机吃午餐。

From a quick look on the internet, and your text being \ 快速浏览一下互联网,您的文字为\

which is here: https://www.fileformat.info/info/unicode/char/f007/index.htm 在这里: https : //www.fileformat.info/info/unicode/char/f007/index.htm

and it says 它说

U+F007 is not a valid unicode character. U + F007不是有效的Unicode字符。

... ...

Android does not support the full Unicode character set and the part of it that it supports depends on the specific device and on where this device is distributed (so, in fact, of the carrier). Android不支持完整的Unicode字符集,它支持的部分取决于特定设备以及此设备的分发位置(实际上是运营商的位置)。

So perhaps the emulator is missing a language pack that your device has? 那么,模拟器可能缺少设备所拥有的语言包? Therefore the string is null . 因此,该字符串为null


A solution may be to look at Downloadable fonts: https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts 一种解决方案可能是查看可下载的字体: https : //developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts

Or to prove that thats the problem, change the text to text="hello world" :-) 或为了证明这就是问题所在,请将文本更改为text="hello world" :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM