我需要访问对话框内的TextView才能更改其属性。我的代码如下:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_execute, null);
builder.setMessage("Execute").setView(view)
.setPositiveButton("Execute", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
filename = view.findViewById(R.id.filename);
[...]
当我运行它时,我得到:
java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView
xml是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialogXfilename"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="notepad.exe"/>
[...]
</android.support.constraint.ConstraintLayout>
我尝试更改布局类型,但是执行代码后应用仍然崩溃
答案 0 :(得分:0)
我刚刚意识到
android:id="@+id/dialogXfilename"
在布局中不是文本视图,这很糟糕