korp korp - 2 months ago 9
Android Question

Android: Getting java.lang.NullPointerException MainActivity.onCreate()

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);
}
}

Answer

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");

                }

            }
        });