-1

I have created an app whereby the user enters a password, clicks the button and it takes them to a new activity. i have made a create password activity where the user first enters a new password and this then takes them to the main activity. in the main activity, the user would type the created password, click the button and it would take them to the next activity. however upon clicking the button after password entry (correct or incorrect) the app crashes. can anyone advise please? Android Manifest below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.junaidandroid.androidsecure">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="com.junaidandroid.CreatePassword">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.junaidandroid.androidsecure.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.junaidandroid.Activity2"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="com.junaidandroid.NoteLauncher">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>
</application>

`

Java Code for MainActivity

public class MainActivity extends AppCompatActivity {

EditText editText;
Button button2;

String password;

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


    //load the password
    SharedPreferences settings = getSharedPreferences("PREFS", 0);
    password = settings.getString("password", "");

    editText = (EditText) findViewById(R.id.editText);
    button2 = (Button) findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = editText.getText().toString();

            if (text.equals(password)){
                //enter the app
                Intent intent = new Intent(getApplicationContext(), Activity2.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(MainActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

}

I HAVE NOW ADDED THE LOGCAT ERROR BELOW:

FATAL EXCEPTION: main Process: com.junaidandroid.androidsecure, PID: 2569 java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.junaidandroid.androidsecure.MainActivity$1.onClick(MainActivity.java:38) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Jay1
  • 29
  • 5

2 Answers2

0

Stab in the dark as you haven't posted your logcat...

editText should be declared final if you want to access it within an inner class. The Exception is being thrown when you try to access it here:

String text = editText.getText().toString();
aliaksei
  • 714
  • 1
  • 9
  • 23
0

Make sure you have write correct findviewbyid of edittext. This null pointer exception is occur just because Of you trying to getting value from edittext which id is not correct.This edit text id should be same as in xml file you have declared.

editText = (EditText)findViewById(R.id.your_edittext_id);
shahid17june
  • 1,441
  • 1
  • 10
  • 15
  • yes, I have managed to resolve the issue, it was exactly how you just pointed out, the ID did not match the one from the XML file hence why it kept crashing – Jay1 Mar 25 '17 at 22:34
  • There is one thing i would like to suggest you when you without entering the password and click the button your app will be crashed so change your if condition like this... if ( !TextUtils.isempty(text) && text.equals(password)){ – shahid17june Mar 25 '17 at 22:45