2016-03-21 37 views
0

我正在爲用戶輸入他們的用戶名到我的應用程序的方式,但我得到此異常。這裏是我的代碼:

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

我已經標記了我得到我的例外的行。

編輯:

這裏是新的錯誤我從

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

改變我的代碼

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

後得到我還聲明後,立即轉移這個說法:

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

我刪除了代碼中的重複項。

的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) 

回答

2

使用Activity context創建AlertDialog

AlertDialog.Builder inputAlert = new AlertDialog.Builder(yourActivity.this); 
+0

這工作得很好......有點。現在,我在該行上得到一個錯誤,指出「IllegalStateException:指定的子項已經有父項,必須先調用子項的父項的removeView()」。我可以稱之爲一次就好。但是當我嘗試第二次調用它時,那是我得到錯誤的時候。 – Brian

+0

@Brian爲什麼你創建了兩個'inputAlert'? –

+0

我在這兩個地方都需要它。如果共享首選項中沒有用戶名,我需要顯示輸入警報,並且如果用戶單擊按鈕更改共享首選項中的現有用戶名,我需要顯示警報。我將inputAlert創建移動到「final TextView usernameTextview ...」的下方,並將它從兩個重複的位置移除,但我仍然得到相同的錯誤。 – Brian

1

我想我最終的解決方案張貼到我的問題的時候。感謝用戶M D我能解決我原來的問題。一些谷歌搜索後,我能夠解決我以後的

問題「IllegalStateException異常:指定的孩子已經有了父母必須在孩子的父母先調用removeView()」

我這樣做通過移動EditText聲明。

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(); 
     } 
    } 
} 
相關問題