-2

I've been searching around the web for two days now and i cant find a solution fort this problem. Im trying to create a simple login system with SQLite. Im on a 32bit Ubuntu and im using Android SDK platform-tools 23.0.1 & Android SDK tools 25.2.2. The app was running fine, untill i created RegisterFragment Class and it crashed at onDestroyView(), i commented it and rebuild the project and now it is giving me the same error at onCreate() in MainActivity Class. Cant fint any specific solution for this error. The problem occurs when i change orientation and crashes at onDestroyView, if i comment it out, it crashes at onCreate, pointing to the clickevent txtSignup.setOnClickListener.

As I wrote, the app was running fine, had no problem with changing orientations though i have layouts for both orientation, including the fragment-layout.

I have tried: -Reinstall JDK8, Android Studio and all SDK-tools and Platform-Tools, Invalidate Caches and Restart,Searching the web for two days.

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.korp.login/com.example.korp.login.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3807) at android.app.ActivityThread.access$800(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5317) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.korp.login.MainActivity.onCreate(MainActivity.java:92) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3807)  at android.app.ActivityThread.access$800(ActivityThread.java:157)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:176)  at android.app.ActivityThread.main(ActivityThread.java:5317)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)  at dalvik.system.NativeStart.main(Native Method)

public class MainActivity extends AppCompatActivity {

    EditText txtUsername;
    EditText txtPassword;
    Button btnLogin;
    TextView txtSignup;

    Context _context;

    String username;
    String password;
    String stored;

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

        if(savedInstanceState!=null){
            username = savedInstanceState.getString("username");
            password = savedInstanceState.getString("password");
        }

        txtUsername = (EditText)findViewById(R.id.editUsername);
        txtPassword = (EditText)findViewById(R.id.editPassword);
        btnLogin = (Button)findViewById(R.id.btnSignIn);
        txtSignup = (TextView)findViewById(R.id.txtCreateAccount);

        txtUsername.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                txtUsername.setHint("");
                return false;
            }
        });

        txtPassword.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                txtPassword.setHint("");
                return false;
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                username = txtUsername.getText().toString();
                password = txtPassword.getText().toString();
                LoginDbAdapter loginDbAdapter = new LoginDbAdapter(_context);
                stored = loginDbAdapter.getSingleEntry(username);

                if(password.equals(stored)){
                    Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(), "Användarnamn och lösenord matcher inte.", Toast.LENGTH_SHORT).show();
                }
            }
        });

        txtSignup.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
            RegisterFragment registerFragment = new RegisterFragment();
            registerFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
            registerFragment.show(getFragmentManager(), "dialog");

                }
        });
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        txtUsername = (EditText) findViewById(R.id.editUsername);
        txtPassword = (EditText)findViewById(R.id.editPassword);
        CharSequence usr = savedInstanceState.getCharSequence("username");
        CharSequence psw = savedInstanceState.getCharSequence("password");
        txtUsername.setText(usr);
        txtPassword.setText(psw);
    }

    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        txtUsername = (EditText)findViewById(R.id.editUsername);
        txtPassword = (EditText)findViewById(R.id.editPassword);
        CharSequence usr = txtUsername.getText();
        CharSequence psw = txtPassword.getText();
        outState.putCharSequence("username", usr);
        outState.putCharSequence("password", psw);
    }

}

RegisterFragment class:

public class RegisterFragment extends DialogFragment {

    EditText regUserName;
    EditText regEmail;
    EditText regPass;
    EditText regPassTwo;
    Button register;
    Button cancel;

    LoginDbAdapter loginDbAdapter;

    String userName;
    String email;
    String password;
    String passwordTwo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.register_fragment, container, false);

        if(savedInstanceState!=null){
            userName = savedInstanceState.getString("username");
            email = savedInstanceState.getString("email");
            password = savedInstanceState.getString("password");
            passwordTwo = savedInstanceState.getString("password2");
        }


        regUserName = (EditText) view.findViewById(R.id.reg_username);
        regEmail = (EditText) view.findViewById(R.id.reg_email);
        regPass = (EditText) view.findViewById(R.id.reg_pass);
        regPassTwo = (EditText) view.findViewById(R.id.reg_pass_two);
        register = (Button)view.findViewById(R.id.btnRegistera);
        cancel = (Button)view.findViewById(R.id.btnCancel);

        register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                userName = regUserName.getText().toString();
                email = regEmail.getText().toString();
                password = regPass.getText().toString();
                passwordTwo = regPassTwo.getText().toString();

                if(userName.equals("")|| email.equals("")||
                   password.equals("")|| passwordTwo.equals("")){
                    Toast.makeText(getActivity().getApplicationContext(), "Var god fyll i alla fält.", Toast.LENGTH_SHORT).show();
                    //return;
                }
                if (!password.equals(passwordTwo)){
                    Toast.makeText(getActivity().getApplicationContext(), "Lösenorden matchar inte.", Toast.LENGTH_SHORT).show();
                    //return;
                }
                if(!email.contains("@")){
                    Toast.makeText(getActivity().getApplicationContext(), "Ogiltig email.", Toast.LENGTH_SHORT).show();
                    //return;
                }
                else{
                    loginDbAdapter.insertEntry(userName, password, email);
                    Toast.makeText(getActivity().getApplicationContext(), "Registreringen lyckades", Toast.LENGTH_SHORT).show();
                }

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            getActivity().getFragmentManager().popBackStack();
            loginDbAdapter.close();

            }
        });



        return view;
    }


    @Override
    public void onDestroyView(){
        super.onDestroyView();
        loginDbAdapter.close();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);

        CharSequence usr = regUserName.getText().toString();
        CharSequence mail = regEmail.getText().toString();
        CharSequence pass = regPass.getText().toString();
        CharSequence passT = regPassTwo.getText().toString();

        savedInstanceState.putCharSequence("username", usr);
        savedInstanceState.putCharSequence("email", mail);
        savedInstanceState.putCharSequence("password", pass);
        savedInstanceState.putCharSequence("password2", passT);
    }    
}
Shaishav
  • 5,282
  • 2
  • 22
  • 41
korp
  • 1
  • 3
  • CharSequence usr = txtUsername.getText(); is where the null pointer is. I would assume that the EditText you're getting the text from is null. – BradleyIW Sep 24 '16 at 20:04
  • The exception i think is in line 92 in MainActivity the txtSignup.setOnClickListener(), but the app crashes only when i change the orientation and unfortunatly RegisterFragment dont save the states..removing .toString() dont solve the problem either. In portrait-mode everything works fine, i can get the fragment and i can back from it without a problem. Its when i change the orientation, with or without the fragment. – korp Sep 25 '16 at 00:36
  • If i comment out txtSignup.setOnClickListener() i can change orientation without problem and the states are saved as it should. I think i do wrong with the fragments lifecycle. – korp Sep 25 '16 at 00:44
  • have you attached configchanges to the activity in your manifest xml? – BradleyIW Sep 25 '16 at 02:50
  • No..what do you suggest? Can you give me an example? – korp Sep 25 '16 at 06:47
  • in your manifest file – BradleyIW Sep 25 '16 at 08:36
  • Yes i have added , the RegisterFragment on the other hand can not be added to the AndroidManifest. As i wrote yesterday it worked before, then something happend... – korp Sep 25 '16 at 12:24
  • I would assume there is a mistake somewhere in your XMLs, or something. The issue here is that the `findViewById()` method is not finding that view on the inflated layout and thus it is null. – Chisko Sep 30 '16 at 00:35
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Chisko Sep 30 '16 at 00:38

1 Answers1

-2

Solution: registerFragment is going to be null because it has not been added (yet) The if-statement solved it, but since FragmentTransition was not added until API level 19 and i am debugging in API level 17, the Logcat is giving me this error first time a tap txtSignup:

Could not find class 'android.transition.Transition'.

But for some reason it works.

txtSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                RegisterFragment registerFragment = new RegisterFragment();
                if(registerFragment !=null) {
                registerFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
                registerFragment.show(getFragmentManager(), "dialog");

                }

            }
        });
korp
  • 1
  • 3
  • Please consider not disregarding errors the IDE is throwing. It is not plotting against you. It is telling you there is a mistake somewhere. – Chisko Sep 30 '16 at 00:36
  • Dont know, it crashed before. Now it dont. – korp Oct 25 '16 at 08:39