Reputation:
I get this strange error, when I activate my clickListener
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadData("test ", "text/html", "utf-8");
webView.loadUrl("https://www.google.de/");
webView.getSettings().setDomStorageEnabled(true);
PrefUtils.setKioskModeActive(true, getApplicationContext());
}
@Override
public void onBackPressed() {
Context context = getApplicationContext();
CharSequence text = "password to deactivate mode!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.dialog_signin);
myDialog.setCancelable(false);
password = (EditText) myDialog.findViewById(R.id.password);
myDialog.show();
// Error probably because of this
Button lbtn = (Button)findViewById(R.id.loginButton);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (password.getText().toString().equals("123")) {
Log.d("myapp", "test1");
} else {
Log.d("myapp", "test2");
}
}
});
}
so basically a dialog textfield windows appears, when I click on the back button. Inside this I check if the password of 123 is correct or not.
Here is my dialog_signin.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif" />
<!--android:hint="@string/password"-->
<Button
android:id="@+id/loginButton"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="@color/red"/>
</LinearLayout>
and this is my activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</WebView>
</RelativeLayout>
Upvotes: 0
Views: 92
Reputation: 316
Replace your code with this :
@Override
public void onBackPressed() {
Context context = getApplicationContext();
CharSequence text = "password to deactivate mode!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.dialog_signin);
myDialog.setCancelable(false);
password = (EditText) myDialog.findViewById(R.id.password);
myDialog.show();
// Error probably because of this
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (password.getText().toString().equals("123")) {
Log.d("myapp", "test1");
} else {
Log.d("myapp", "test2");
}
}
});
Upvotes: 0
Reputation: 18112
You need to get the Button
from the view it is in. If you use findViewById
without any view reference then it would try to find the view in you activity xml in this case activity_main.xml
. loginButton
is not in this xml but in the dialog you have created that's why you are getting NPE. So, change
Button lbtn = (Button)findViewById(R.id.loginButton);
to
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
Upvotes: 1
Reputation: 1074
You are attempting for find the Button in your Activity or Fragment layout, not in the Dialog:
Button lbtn = (Button)findViewById(R.id.loginButton);
should be
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
Upvotes: 3