0

I am getting this exception when trying to start a new activity through a method in my Main

 java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context android.content.Context.getApplicationContext()
' on a null object reference

The method i am using to start the new activity is not called in the main activity. I think this is what is causing the issue, since i tried to call the method in the OnCreate method of the main activity, and it worked.

However i cannot figure out a way to fix this, a friend suggested that i used singleton, but that didn't work - or maybe my understanding of how to use it is just too limited.

This is my main class:

package gruppe16.sdupay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class Main extends Activity {


    static EditText et1, et2, et3, et4;
    private String password = "1234";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.editText1);
        et2 = (EditText) findViewById(R.id.editText2);
        et3 = (EditText) findViewById(R.id.editText3);
        et4 = (EditText) findViewById(R.id.editText4);
        et1.addTextChangedListener(new CustomTextWatcher(et1));
        et2.addTextChangedListener(new CustomTextWatcher(et2));
        et3.addTextChangedListener(new CustomTextWatcher(et3));
        et4.addTextChangedListener(new CustomTextWatcher(et4));
    }

    public void login (String passInput){
        if(passInput.equals(password)){
            Log.d("asd", "PASS IS CORRECT");
            Intent appIntent = new Intent(getApplicationContext(), AppActivity.class);
            startActivity(appIntent);

        } else {
            Log.d("asd2", "PASS IS NOT CORRECT");
        }

    }



}

This is my CustomTextWatcher class

package gruppe16.sdupay;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

/**
 * Created by jonaspedersen on 15/12/15.
 */
public class CustomTextWatcher implements TextWatcher {

    static String et1String, et2String, et3String, et4String;
    private EditText mEditText;
    private Main MainObject = new Main();
    private String passString;

    public CustomTextWatcher(EditText e) {
        mEditText = e;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (mEditText == Main.et1){
            et1String = Main.et1.getText().toString();
            Main.et2.requestFocus();
        } else if (mEditText == Main.et2){
            et2String = Main.et2.getText().toString();
            Main.et3.requestFocus();
        } else if (mEditText == Main.et3){
            et3String = Main.et3.getText().toString();
            Main.et4.requestFocus();
        } else if (mEditText == Main.et4){
            et4String = Main.et4.getText().toString();
            passString = et1String + et2String + et3String + et4String;
            MainObject.login(passString);

        }
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gruppe16.sdupay">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Light.NoTitleBar">
        <activity
            android:name=".Main"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/SDUTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".AppActivity"
            android:label="@string/title_activity_app"></activity>
    </application>

</manifest>

1 Answers1

1

use

Intent appIntent = new Intent(Main.this, AppActivity.class);

also change CustomTextWatcher constructor

private Main MainObject;

private String passString;

public CustomTextWatcher(EditText e,Main MainObject) {
    mEditText = e;
    this.MainObject = MainObject;
}

and set TextWatcher like

et1.addTextChangedListener(new CustomTextWatcher(et1),Main.this);
Rohit Patil
  • 1,068
  • 8
  • 9