私はユーザーに自分のユーザー名をアプリケーションに入力する方法を検討中ですが、この例外が発生しています。ここに私のコードです:
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(MainActivity.this);
に
AlertDialog.Builder inputAlert = new AlertDialog.Builder(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なぜ2つの 'inputAlert'を作成しましたか? –
私は両方の場所でそれが必要です。共有の設定にユーザー名がない場合、入力警告を表示する必要があります。ユーザーが共有の設定で既存のユーザー名を変更するためにボタンをクリックすると、警告を表示する必要があります。私はinputAlertの作成を "final TextView usernameTextview ..."の直下に移動し、2つの重複した場所から削除しましたが、同じエラーが表示されます。 – Brian