Unable to install OnClickListener in Android application dialog

I cannot install OnClickListener in the dialog box of an Android application. It ends with an error. Journal:

01-10 12:35:45.792: ERROR/AndroidRuntime(918): FATAL EXCEPTION: main 01-10 12:35:45.792: ERROR/AndroidRuntime(918): java.lang.NullPointerException 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.htmleditor.MainActivity.onCreateDialog(MainActivity.java:169) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.app.Activity.onCreateDialog(Activity.java:2482) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.app.Activity.createDialog(Activity.java:882) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.app.Activity.showDialog(Activity.java:2557) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.app.Activity.showDialog(Activity.java:2524) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.htmleditor.MainActivity$2.onClick(MainActivity.java:106) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.view.View.performClick(View.java:2485) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.view.View$PerformClick.run(View.java:9080) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.os.Handler.handleCallback(Handler.java:587) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.os.Handler.dispatchMessage(Handler.java:92) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.os.Looper.loop(Looper.java:123) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at android.app.ActivityThread.main(ActivityThread.java:3647) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at java.lang.reflect.Method.invokeNative(Native Method) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at java.lang.reflect.Method.invoke(Method.java:507) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-10 12:35:45.792: ERROR/AndroidRuntime(918): at dalvik.system.NativeStart.main(Native Method) 01-10 12:35:48.002: ERROR/InputDispatcher(67): channel '4065a710 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8 01-10 12:35:48.012: ERROR/InputDispatcher(67): channel '4065a710 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! 

I have two versions of the code. Both of them cause an error.

MainActivity, Oncreate () function

 insertImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Dialog dialog = new Dialog(MainActivity.this); dialog.setContentView(R.layout.dialogimage); dialog.setTitle("Insert image"); final EditText fieldForURL = (EditText)findViewById(R.id.fieldForURL); Button on = (Button)findViewById(R.id.okImage); on.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { visualPane.getText().insert(visualPane.getSelectionEnd(),"<img src=\"" + fieldForURL.getText().toString() + "\">"); } }); dialog.setCancelable(true); dialog.show(); //showDialog(IDD_CUSTOM_INSERT_IMAGE); } }); 

or function OnCreateDialog ()

 @Override protected Dialog onCreateDialog(int id) { LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Create table"); alert.setCancelable(false); Dialog dialog = null; dialog = new Dialog(this); switch (id) { case IDD_CUSTOM_INSERT_TABLE: dialog.setTitle("Insert table"); dialog.setContentView(R.layout.dialogtable); okTableDialog = (Button)findViewById(R.id.okTable); cancelTableDialog = (Button)findViewById(R.id.okTable); fieldForRows = (EditText)findViewById(R.id.entry01); fieldForColumns = (EditText)findViewById(R.id.entry02); fieldForWidthOfBroder = (EditText)findViewById(R.id.fieldForBorder); okTableDialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TableAction tablAc = new TableAction(visualPane); tablAc.insertTable(fieldForRows.getText().toString(),fieldForColumns.getText().toString(), fieldForWidthOfBroder.getText().toString()); } }); return dialog; case IDD_CUSTOM_INSERT_IMAGE: dialog.setTitle("Insert image"); dialog.setContentView(R.layout.dialogimage); final EditText fieldForURL = (EditText)findViewById(R.id.fieldForURL); Button on = (Button)findViewById(R.id.okImage); on.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { visualPane.getText().insert(visualPane.getSelectionEnd(),"<img src=\"" + fieldForURL.getText().toString() + "\">"); } }); return dialog; default: return alert.create(); } } 

If I comment on "setOnClickListener" in both cases, the program will not fail

Please help, why is this happening?

+4
source share
1 answer

You are not looking for a button for the desired object. Try the following:

 Button on = (Button)dialog.findViewById(R.id.okImage); 
+18
source

Source: https://habr.com/ru/post/1334923/


All Articles