我正在爲用戶輸入他們的用戶名到我的應用程序的方式,但我得到此異常。這裏是我的代碼:
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)
這工作得很好......有點。現在,我在該行上得到一個錯誤,指出「IllegalStateException:指定的子項已經有父項,必須先調用子項的父項的removeView()」。我可以稱之爲一次就好。但是當我嘗試第二次調用它時,那是我得到錯誤的時候。 – Brian
@Brian爲什麼你創建了兩個'inputAlert'? –
我在這兩個地方都需要它。如果共享首選項中沒有用戶名,我需要顯示輸入警報,並且如果用戶單擊按鈕更改共享首選項中的現有用戶名,我需要顯示警報。我將inputAlert創建移動到「final TextView usernameTextview ...」的下方,並將它從兩個重複的位置移除,但我仍然得到相同的錯誤。 – Brian