tazmanianDeviloper
tazmanianDeviloper

Reputation: 1

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. My Android Device is running on Lollipop (5.1.1) at 22 API, my AVD is running on Pie (9.0) at 28 API. I have set the minimum SDK to 21 and the compile SDK to 28 in my gradle.

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)

Which points to line 140 of the FirstActivity:

String avatarInput = playerAvatar.getText().toString();

which is referencing an XML attribute and assigning its text value to a String. Now I am sure that the XML has in fact a text value for 2 reasons:

1) I have hard-coded that text value myself.

2) line 140 does not return null on my Android Device.

The problem therefore must be from the 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 {

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. 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.

Upvotes: 0

Views: 63

Answers (1)

Blundell
Blundell

Reputation: 76506

From a quick look on the internet, and your text being \uf007

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

and it says

U+F007 is not a valid unicode character.

...

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).

So perhaps the emulator is missing a language pack that your device has? Therefore the string is null.


A solution may be to look at 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" :-)

Upvotes: 2

Related Questions