I saw many similar questions online, and tried several solutions. But none fixed my issue. One important thing I want to mention is that, my code works well on android Phones, it only crashes on android Tablets. I have no idea what's going on.
This is the stack:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx.android/com.xxxx.xxxx.android.LoginScreenActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.xxxx.xxxx.android.LoginScreenActivity.onCreate(LoginScreenActivity.java:89)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
... 10 more
This is the LoginScreenActivity:
public class LoginScreenActivity extends Activity implements OnClickListener
{
private Context context;
private CheckBox showPassword;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
context = getApplicationContext();
showPassword = (CheckBox)findViewById(R.id.checkbox_show_password);
//below is line 89 of LoginScreenActivity, exception happened here.
showPassword.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
//.................
}
});
}
}
Here's the xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_show_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Password"/>
</RelativeLayout>
</ScrollView>