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)