2017-08-23 2 views
-2

Si vous tentez de définir un écouteur de clic sur le boîtier de commutateur qui reviendra avec une boîte de dialogue personnalisée avec un texte spécifique, cela dépend du cas. lorsque je lance l'application et je clique sur un bouton responsable d'afficher le dialogue avec un résultat spécifique, l'application se bloque.SetOnClickListener pour cas de commutateur de boîte de dialogue personnalisée

Quand je vérifie le débogueur où l'erreur pourrait être, il va à la ligne où j'essaye de changer le texte de la boîte de dialogue. par exemple ceci:

gm.setText("result6"); 

Le code Java

private void getHint(final int viewId) { 
    int remainHints = Integer.parseInt(hintsValue.getText().toString()); 
    if (isHintUsed(viewId) != 1) { 
     if (remainHints < 8) { 
      final Dialog dialog = new Dialog(TestActivity.this); 
      // Include dialog.xml file 
      dialog.setContentView(R.layout.dialog); 
      dialog.show(); 
      Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel); 
      // if decline button is clicked, close the custom dialog 
      declineButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // Close dialog 
        dialog.dismiss(); 
       } 
      }); 



     }else { 
      if (viewId == R.id.letter && isLetterHintOn == 1) { 
       stopLetterHint(); 
      } else { 
       final Dialog dialog = new Dialog(TestActivity.this); 
       dialog.setContentView(R.layout.dialog); 

       switch (viewId) { 
        case R.id.mom: 
         TextView mom = (TextView) findViewById(R.id.tv_description); 
         mom.setText("result 1"); 
         break; 
        case R.id.dad: 
         TextView dad = (TextView) findViewById(R.id.tv_description); 
         dad.setText("result 2"); 
         break; 

        case R.id.brother: 
         TextView brother = (TextView) findViewById(R.id.tv_description); 
         brother.setText("result 3"); 
         break; 

        case R.id.sister: 
         TextView sis = (TextView) findViewById(R.id.tv_description); 
         sis.setText("result 4"); 
         break; 

        case R.id.gfather: 
         TextView gf = (TextView) findViewById(R.id.tv_description); 
         gf.setText("result 5"); 
         break; 

        case R.id.gmother: 
         TextView gm = (TextView) findViewById(R.id.tv_description); 
         gm.setText("result 6"); 
         break; 
       } 

       Button videoButton = (Button) dialog.findViewById(R.id.btn_video); 
       // if decline button is clicked, close the custom dialog 
       videoButton.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View view) { 
         if (viewId != R.id.letter) { 
          db.addUsedHint(); 
          hintsValue.setText(String.valueOf(getHintsNumber())); 
         } 
         executeHint(viewId); 
        } 

       }); 

       Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel); 
       // if decline button is clicked, close the custom dialog 
       declineButton.setOnClickListener(new OnClickListener() { 

        public void onClick(View v) { 
        } 

       }); 

       dialog.show(); 
      } 

     } 

    } else { 
     executeHint(viewId); 
    } 

} 

Le fichier xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white"> 

<RelativeLayout 
    android:id="@+id/rl_quit_learning" 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:background="@drawable/popup" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp"> 

    <TextView 
     android:id="@+id/tv_quit_learning" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_margin="10dp" 
     android:textColor="#000" 
     android:text="Quit LEarning?" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tv_description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_quit_learning" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:textColor="#000" 
     android:gravity="center" 
     android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?" 
     android:textSize="16sp" /> 
</RelativeLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="125dp" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/btn_cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/lock" /> 

    <Button 
     android:id="@+id/btn_video" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/lock" /> 

</LinearLayout> 

LogCat

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.momdad.family, PID: 18297 
       java.lang.NullPointerException: Attempt to invoke virtual 
method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
        at com.momdad.family 
.TestActivity.getHint(TestActivity.java:1171) 
        at com.momdad.family 
.TestActivity.access$200(TestActivity.java:67) 
        at com.momdad.family 
.TestActivity$12.onClick(TestActivity.java:1059) 
        at android.view.View.performClick(View.java:5702) 
        at 
android.view.View$PerformClick.run(View.java:22533) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
+0

afficher les logcat. –

+0

@ AsifPatel J'ai ajouté le logcat. et merci –

Répondre

0

Remplacez votre code avec ce code:

switch (viewId) { 
       case R.id.mom: 
        TextView mom = (TextView) dialog.findViewById(R.id.tv_description); 
        mom.setText("result 1"); 
        break; 
       case R.id.dad: 
        TextView dad = (TextView) dialog.findViewById(R.id.tv_description); 
        dad.setText("result 2"); 
        break; 

       case R.id.brother: 
        TextView brother = (TextView) dialog.findViewById(R.id.tv_description); 
        brother.setText("result 3"); 
        break; 

       case R.id.sister: 
        TextView sis = (TextView) dialog.findViewById(R.id.tv_description); 
        sis.setText("result 4"); 
        break; 

       case R.id.gfather: 
        TextView gf = (TextView) dialog.findViewById(R.id.tv_description); 
        gf.setText("result 5"); 
        break; 

       case R.id.gmother: 
        TextView gm = (TextView) dialog.findViewById(R.id.tv_description); 
        gm.setText("result 6"); 
        break; 
      }