0

I am trying to work on an application to read SA citizenship ID on Android Studio but when I run the application it is not even opening?

The Source Code :

package com.example.magicidapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText etID;
Button btnSubmit;
TextView tvResults;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etID = findViewById(R.id.etID);
    btnSubmit = findViewById(R.id.btnSubmit);
    tvResults = findViewById(R.id.tvResults);

    tvResults.setVisibility(View.GONE);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String idNumber = etID.getText().toString().trim();

            String dob =idNumber.substring(0,6);

            int gender = Integer.parseInt(Character.toString(idNumber.charAt(6)));

            String sGender;

            if(gender<5)
                sGender = getString(R.string.female);
            else
                sGender = getString(R.string.male);

            int nationality = Integer.parseInt(Character.toString(idNumber.charAt(10)));

            String sNationality;

            if(nationality == 0)
                sNationality = getString(R.string.sacit);
            else
                sNationality = getString(R.string.permanentresident);

            String text = getString(R.string.dob) + dob + 
getString(R.string.newline) +
                    getString(R.string.gender) + sGender + 
getString(R.string.newline) +
                    getString(R.string.nationality) + sNationality;

            tvResults.setText(text);

            tvResults.setVisibility(View.VISIBLE);
            }
        });
    }
}

Error :

2019-03-09 17:34:40.900 5053-5053/com.example.magicidapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.magicidapplication, PID: 5053 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magicidapplication/com.example.magicidapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0)

I got error like this what to do.

questionasker
  • 2,536
  • 12
  • 55
  • 119
Pradeep
  • 25
  • 6

1 Answers1

2

There is lifecycle of activity execution, then, when you want to execute an activity, you must implement the right methods of the activity at the right time, as for example, the onCreate() method is used to initialize the activity, but overload the visible parts of GUI, it starts from the running of the onStart() method and ends (displaying visible parts) when running the onResume() method, so, when onCreate() or onStart() is crashed (caused by Exception or others), so nothing appeared !

Understand the Activity Lifecycle From the address : https://developer.android.com/guide/components/activities/activity-lifecycle