2011-01-10 2 views
4

Je ne peux pas définir OnClickListener dans une boîte de dialogue de l'application android. Il finit avec une erreur. Le journal est:Impossible de définir OnClickListener dans une boîte de dialogue de l'application android

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! 

J'ai deux versions du code. Les deux d'entre eux provoque l'erreur

MainActivity, la fonction 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); 

      } 
     }); 

ou la fonction 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(); 
    } 
} 

Si je commente-out "setOnClickListener" dans les deux cas, le Programme sera pas finir avec une erreur

S'il vous plaît, aidez, pourquoi ça arrive?

Répondre

18

Vous recherchez le bouton pas pour le bon objet. Essayez ceci:

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

Je suis très obligé de vous – user565447

+0

ne soyez pas obligé, il suffit de voter et d'accepter la réponse! =) –

+0

Comment voter si j'ai une mauvaise réputation? – user565447

Questions connexes