Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set OnClickListener in a dialog of the android application

Tags:

android

I can't set OnClickListener in a dialog of the android application. It finishes with an error. The log is:

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 provoke the error

MainActivity, function Oncreate()

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-out "setOnClickListener" in both cases, the programm will not finish with an error

Please help, why it happens?

like image 333
user565447 Avatar asked Dec 02 '22 03:12

user565447


1 Answers

You're searching the button not for the right object. Try this:

Button on = (Button)dialog.findViewById(R.id.okImage);
like image 117
Vladimir Ivanov Avatar answered May 23 '23 12:05

Vladimir Ivanov