如何修复片段上的java.lang非法状态异常?
我制作了一个核对表应用程序,当用户将检查添加到该应用程序时,我得到以下错误和崩溃。有人能提供一些建议来解决这个问题吗?当我使用片段时,我创建了第二个Java文件,并将其引用到XML中,因为在一个Java中不能有多个类,我尝试了合并它们,但出现了很多错误,并且不能多次使用公共类“extends” 这是错误 这里是XML如何修复片段上的java.lang非法状态异常?,java,android,xml,Java,Android,Xml,我制作了一个核对表应用程序,当用户将检查添加到该应用程序时,我得到以下错误和崩溃。有人能提供一些建议来解决这个问题吗?当我使用片段时,我创建了第二个Java文件,并将其引用到XML中,因为在一个Java中不能有多个类,我尝试了合并它们,但出现了很多错误,并且不能多次使用公共类“extends” 这是错误 这里是XML <TextView android:id="@+id/text_checklist" android:layout_width="match_parent"
<TextView
android:id="@+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="@font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="@string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="@+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="@string/buttonText" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
这是第一个Java(片段)
checklistViewModel=
ViewModelProviders.of(this.get)(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
视图根=充气机。充气(R.layout.fragment_检查表,容器,错误);
最终文本视图文本视图=root.findViewById(R.id.text\u检查表);
checklistViewModel.getText().observe(这个,新的观察者(){
@凌驾
公共void onChanged(@Nullable String s){
textView.setText;
}
});
返回根;
}
}
这是第二份Java(清单)
按钮btn\u检查表;
编辑文本输入文本;
列表视图列表视图;
数组列表;
公共无效添加按钮2(视图){
String text=inputText.getText().toString();
列表。添加(文本);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,list);
setAdapter(适配器);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_检查表);
btn检查表=findViewById(R.id.检查列表按钮);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
列表=新的ArrayList();
}
}将
tools:context=“YourActivityName”
添加到XML布局的根目录中。也许这对你有所帮助,你已经补充说了,仍然没有修复错误我知道有一些碎片需要处理,因为我把清单移到了一个没有碎片的空白应用程序中,它工作了,只是不确定在哪里。
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}