我正在研究一种让用户在我的应用程序中输入用户名的方法,但是我遇到了这个异常。这是我的代码:
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);
我删除了代码中的重复项。
日志猫:
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)
创建AlertDialog
时使用Activity context
AlertDialog.Builder inputAlert = new AlertDialog.Builder(yourActivity.this);
我想
发布我问题的最终解决方案。感谢用户 M D,我能够解决我原来的问题。经过一些谷歌搜索,我能够解决我随后的问题
"非法状态例外:指定的子项已有父项。您必须先在孩子的父级上调用 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();
}
}
}