2016-03-21 10 views
0

Kullanıcının kullanıcı adını benim uygulamamıza girmesi için bir yol üzerinde çalışıyorum, ancak bu istisnayı alıyorum. İşte kodum:

package myPackage; 

public class MainActivity extends AppCompatActivity { 

    private Context myAppContext; 
    private String username; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     myAppContext = getApplicationContext(); 

     // Username Input Dialog 
     final EditText userInput = new EditText(this); 
     final SharedPreferences myAppPreferences = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE); 
     final TextView usernameTextview = (TextView)findViewById(R.id.username_box); 

     username = myAppPreferences.getString("username", null); 

     usernameTextview.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(username != null){ 
        userInput.setText(username); 
       } 

       AlertDialog.Builder inputAlert = new AlertDialog.Builder(myAppContext); 
       inputAlert.setIcon(R.drawable.alert_50x50); 
       inputAlert.setTitle("Username"); 
       inputAlert.setMessage("Please enter your username."); 
       inputAlert.setView(userInput); 
       inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         SimpleDateFormat sdf = new SimpleDateFormat(Constants.STANDARD_FILE_TIMESTAMP, Locale.US); 
         username = (!userInput.getText().toString().equalsIgnoreCase("")) ? userInput.getText().toString() : Constants.TABLET_ID; 
         SharedPreferences.Editor editor = myAppPreferences.edit(); 
         editor.putString("username", username); 
         editor.putString("log-in date", sdf.format(new Date())); 
         editor.apply(); 
         usernameTextview.append(username); 
        } 
       }); 
       inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         usernameTextview.append(username); 
         dialog.dismiss(); 
        } 
       }); 
       AlertDialog alertDialog = inputAlert.create(); 
       // THE FOLLOWING LINE IS WHERE I GET MY WindowManager$BadTokenException 
       alertDialog.show(); 
      } 
     }); 

     if(username != null){ 
      usernameTextview.append(username); 
     } else { 
      AlertDialog.Builder inputAlert = new AlertDialog.Builder(this); 
      inputAlert.setIcon(R.drawable.alert_50x50); 
      inputAlert.setTitle("Username"); 
      inputAlert.setMessage("Please enter your username."); 
      inputAlert.setView(userInput); 
      inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.STANDARD_FILE_TIMESTAMP, Locale.US); 
        username = (!userInput.getText().toString().equalsIgnoreCase("")) ? userInput.getText().toString() : Constants.TABLET_ID; 
        SharedPreferences.Editor editor = myAppPreferences.edit(); 
        editor.putString("username", username); 
        editor.putString("log-in date", sdf.format(new Date())); 
        editor.apply(); 
        usernameTextview.append(username); 
       } 
      }); 
      inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        usernameTextview.append(username); 
        dialog.dismiss(); 
       } 
      }); 
      AlertDialog alertDialog = inputAlert.create(); 
      alertDialog.show(); 
     } 
    } 
} 

Özel durumumu aldığım satırı etiketledim.

DÜZENLEME: İşte

Ben de deyiminden sonra hemen bu açıklamayı taşındı ben

AlertDialog.Builder inputAlert = new AlertDialog.Builder(MainActivity.this); 

için

AlertDialog.Builder inputAlert = new AlertDialog.Builder(this); 

benim kod değiştirdikten sonra alıyorum yeni hatadır :

final TextView usernameTextview = (TextView)findViewById(R.id.username_box); 

Ve koddaki yinelemeleri kaldırdım.

logcat:

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a7c1f8) 
E/AndroidRuntime: FATAL EXCEPTION: main 
E/AndroidRuntime: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
E/AndroidRuntime:  at android.view.ViewGroup.addViewInner(ViewGroup.java:3337) 
E/AndroidRuntime:  at android.view.ViewGroup.addView(ViewGroup.java:3208) 
E/AndroidRuntime:  at android.view.ViewGroup.addView(ViewGroup.java:3188) 
E/AndroidRuntime:  at com.android.internal.app.AlertController.setupView(AlertController.java:401) 
E/AndroidRuntime:  at com.android.internal.app.AlertController.installContent(AlertController.java:241) 
E/AndroidRuntime:  at android.app.AlertDialog.onCreate(AlertDialog.java:336) 
E/AndroidRuntime:  at android.app.Dialog.dispatchOnCreate(Dialog.java:353) 
E/AndroidRuntime:  at android.app.Dialog.show(Dialog.java:257) 

E/AndroidRuntime:  at myPackage.main.MainActivity$2.onClick(MainActivity.java:138) 

E/AndroidRuntime:  at android.view.View.performClick(View.java:3511) 
E/AndroidRuntime:  at android.view.View$PerformClick.run(View.java:14105) 
E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:605) 
E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:92) 
E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:4424) 
E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 

cevap

2

Kullanım Activity contextAlertDialog

Problemimin benim son çözüm sonrası istedim
AlertDialog.Builder inputAlert = new AlertDialog.Builder(yourActivity.this); 
+0

Bu iyi çalıştı ... bir çeşit. Şimdi bu satırda "IllegalStateException: Belirtilen çocuğun zaten bir üst bilgisi var" şeklinde bir hata alıyorum. Önce çocuğun üstündeki removeView() öğesini çağırmalısınız. Sadece bir kere iyi diyebilirim. Ama bunu ikinci kez aramaya çalıştığımda, hatayı aldığımda. – Brian

+0

@Brian Neden iki "inputAlert" oluşturdunuz? –

+0

Her iki yerde de buna ihtiyacım var. Paylaşılan tercihlerde kullanıcı adı yoksa giriş uyarısı gösterisine ihtiyacım var ve bir kullanıcı paylaşılan tercihlerdeki mevcut kullanıcı adını değiştirmek için düğmeyi tıklarsa uyarı gösterimi almam gerekir. InputAlert yaratımını "son TextView usernameTextview ..." in hemen altına taşıdım ve iki yinelenen yerden kaldırdım, ancak yine de aynı hatayı alıyorum. – Brian

1

yaratır. M D kullanıcısına teşekkürler. Orijinal sorunumu düzelttim. Bazı googling sonra

benim müteakip sorunu çözmek başardı "IllegalStateException:. Belirtilen çocuk zaten Önce çocuğun ebeveyni üzerindeki removeView() bir üst çağırmalıdır etti"

Bunu yapan EditText bildirimlerini hareket ettirerek.

package myPackage; 

public class MainActivity extends AppCompatActivity { 

    private String username; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final SharedPreferences myAppPreferences = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE); 
     final TextView usernameTextview = (TextView)findViewById(R.id.username_box); 

     username = myAppPreferences.getString("username", null); 

     usernameTextview.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final EditText userInput = new EditText(MainActivity.this); 
       if(username != null){ 
        userInput.setText(username); 
       } 

       AlertDialog.Builder inputAlert = new AlertDialog.Builder(MainActivity.this); 
       inputAlert.setIcon(R.drawable.alert_50x50); 
       inputAlert.setTitle("Username"); 
       inputAlert.setMessage("Please enter your username."); 
       inputAlert.setView(userInput); 
       inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         SimpleDateFormat sdf = new SimpleDateFormat(Constants.STANDARD_FILE_TIMESTAMP, Locale.US); 
         username = (!userInput.getText().toString().equalsIgnoreCase("")) ? userInput.getText().toString() : Constants.TABLET_ID; 
         SharedPreferences.Editor editor = myAppPreferences.edit(); 
         editor.putString("username", username); 
         editor.putString("log-in date", sdf.format(new Date())); 
         editor.apply(); 
         usernameTextview.setText(username); 
        } 
       }); 
       inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         usernameTextview.setText(username); 
         dialog.dismiss(); 
        } 
       }); 
       AlertDialog alertDialog = inputAlert.create(); 
       alertDialog.show(); 
      } 
     }); 

     if(username != null){ 
      usernameTextview.setText(username); 
     } else { 
      final EditText userInput = new EditText(this); 
      AlertDialog.Builder inputAlert = new AlertDialog.Builder(MainActivity.this); 
      inputAlert.setIcon(R.drawable.alert_50x50); 
      inputAlert.setTitle("Username"); 
      inputAlert.setMessage("Please enter your username."); 
      inputAlert.setView(userInput); 
      inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.STANDARD_FILE_TIMESTAMP, Locale.US); 
        username = (!userInput.getText().toString().equalsIgnoreCase("")) ? userInput.getText().toString() : Constants.TABLET_ID; 
        SharedPreferences.Editor editor = myAppPreferences.edit(); 
        editor.putString("username", username); 
        editor.putString("log-in date", sdf.format(new Date())); 
        editor.apply(); 
        usernameTextview.setText(username); 
       } 
      }); 
      inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        usernameTextview.setText(Constants.TABLET_ID); 
        dialog.dismiss(); 
       } 
      }); 
      AlertDialog alertDialog = inputAlert.create(); 
      alertDialog.show(); 
     } 
    } 
} 
İlgili konular