Answer the question
In order to leave comments, you need to log in
Dialog why am I getting a NulPointerException?
Hello. I am writing a password manager application, I implemented the password generation function in it. In order for the generator to work, the following parameters must be passed to it (password length, ABC, 123 and symbols). I want to implement these functions in a dialog box with layout.dialog_signin.xml set in advance.
public void dialogPanel(){
final AlertDialog.Builder mBulder = new AlertDialog.Builder(this);
View linearlayout = getLayoutInflater().inflate(R.layout.dialog_signin, null);
mBulder.setView(linearlayout);
final NumberPicker numberPicker = (NumberPicker) getWindow().findViewById(R.id.numberPicker);
numberPicker.setMinValue(6);
numberPicker.setMaxValue(16);
checkABC = (CheckBox) findViewById(R.id.checkABC);
check123 = (CheckBox) findViewById(R.id.check123);
checkSymbol = (CheckBox) findViewById(R.id.checkSymbol);
Button dialogSave = (Button) getWindow().findViewById(R.id.dialogSave);
dialogSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"работает",Toast.LENGTH_SHORT).show();
}
});
mBulder.create();
mBulder.show();
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/dialogNumberText">
<TextView
android:text="Generator"
android:layout_width="match_parent"
android:id="@+id/textView7"
android:textSize="24sp"
android:gravity="center"
android:background="#FFFFBB33"
android:textColor="#ffffff"
android:fontFamily="sans-serif"
android:textStyle="normal|bold" android:layout_height="50dp"/>
<NumberPicker
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/numberPicker"/>
<CheckBox
android:text="Заглавные буквы"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkABC"/>
<CheckBox
android:text="Цифры"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/check123"/>
<CheckBox
android:text="Символы"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkSymbol"/>
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialogSave" />
</LinearLayout>
//исключение
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.comand.key, PID: 30520
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference
at com.example.comand.key.AddActivity.dialogPanel(AddActivity.java:96)
at com.example.comand.key.AddActivity$1.onCheckedChanged(AddActivity.java:53)
at android.widget.CompoundButton.setChecked(CompoundButton.java:154)
at android.widget.CompoundButton.toggle(CompoundButton.java:113)
at android.widget.CompoundButton.performClick(CompoundButton.java:118)
at android.view.View$PerformClick.run(View.java:19911)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5275)
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:910)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
Answer the question
In order to leave comments, you need to log in
findViewById must be called on the view, in this case the linearlayout variable.
Look for views in linearlayat. In general, the alertdialog is the bottom, use the dialogfragment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question