这是错误
public class currentjobAdapter extends BaseAdapter {
String[] Reg;
Context C;
public currentjobAdapter() {
Reg = null;
}
public currentjobAdapter(Context c, String[] reg){
this.C = c;
Reg = reg;
}
@Override
public int getCount() {
return Reg.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
final View row;
if (convertView == null) {
row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.YourCustomList, viewGroup, false);
} else {
row = convertView;
}
TextView tv1 = row.findViewById(R.id.yourid);
tv1.setText(Reg[i]);
final String[] date = new String[1];
String registration = Reg[i];
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference dbref = db.getReference().child(registration).child(registration);
dbref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
date[0] = dataSnapshot.child("date").getValue(String.class);
TextView tv2 = (TextView) row.findViewById(R.id.yourid2);
tv2.setText(date[0]);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return row;
}
以下是有问题的代码行
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = Group@5ea0c6fc
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Unknown Source)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(Unknown Source)
at project1.Crab.setCrab(Crab.java:45)
at project1.SeaAnimals.addCrab(SeaAnimals.java:65)
at project1.ProjectOne.mouseClick(ProjectOne.java:133)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8300(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
public class Crab {
private final Image c = new Image("File:crab.gif");
private ArrayList<ImageView> crabList = new ArrayList<ImageView>();
private int _counter;
private Group _crab = new Group();
/** Constructor <br>
* Initializes counter to -1
*/
public Crab() {
_counter = -1;
}
/** Setter <br>
* Adds a crab to the array list
*/
public void addCrab() {
if(_counter < 5) {
crabList.add(new ImageView(c));
_counter++;
}
}
/** Setter <br>
* Deletes a crab from the array list
*/
public void deleteCrab() {
if(_counter > 0) {
crabList.remove(_counter);
_counter--;
}
}
/** Getter <br>
*
* @return returns crab group
*/
public Group setCrab() {
if(_counter >= 0) { //adds the first crab
crabList.get(0).setTranslateX(80);
crabList.get(0).setTranslateY(500);
crabList.get(0).setFitHeight(100);
crabList.get(0).setFitWidth(100);
_crab.getChildren().add(crabList.get(0));
if(_counter >= 1) { //adds the second crab
crabList.get(1).setTranslateX(230);
crabList.get(1).setTranslateY(500);
crabList.get(1).setFitHeight(100);
crabList.get(1).setFitWidth(100);
_crab.getChildren().add(crabList.get(1));
if (_counter >= 2) { // adds the third crab
crabList.get(2).setTranslateX(380);
crabList.get(2).setTranslateY(500);
crabList.get(2).setFitHeight(100);
crabList.get(2).setFitWidth(100);
_crab.getChildren().add(crabList.get(2));
if (_counter >= 3) { //adds the fourth crab
crabList.get(3).setTranslateX(530);
crabList.get(3).setTranslateY(500);
crabList.get(3).setFitHeight(100);
crabList.get(3).setFitWidth(100);
_crab.getChildren().add(crabList.get(3));
if (_counter >= 4) { //adds the fifth crab
crabList.get(4).setTranslateX(680);
crabList.get(4).setTranslateY(500);
crabList.get(4).setFitHeight(100);
crabList.get(4).setFitWidth(100);
_crab.getChildren().add(crabList.get(4));
if (_counter == 5) { //adds the sixth crab
crabList.get(5).setTranslateX(830);
crabList.get(5).setTranslateY(500);
crabList.get(5).setFitHeight(100);
crabList.get(5).setFitWidth(100);
_crab.getChildren().add(crabList.get(5));
}
}
}
}
}
}
return _crab;
}
}
public Group addCrab() {
_crab.addCrab();
_gCrab = _crab.setCrab();
return _gCrab;
}
public Group deleteCrab() {
_crab.deleteCrab();
_gCrab = _crab.setCrab();
return _gCrab;
}
}
我不知道如何解决问题,也不知道问题所在。任何帮助表示赞赏。
答案 0 :(得分:0)
您的setCrab
方法尝试将所有螃蟹图像添加到Group
而不删除任何子项。这样,如果在您尝试再次添加它们之前Group
中有螃蟹,则会导致异常,因为JavaFX不允许这样做。
您还需要确保永远不要将同一节点两次添加到同一个父节点。
此外,从setCrab
类的用户隐藏Crab
会降低使用该类的复杂性。为什么当1个调用执行相同的工作时,用户调用2个方法。您甚至可以编写一个CrabGroup
类作为节点本身,并避免将Group
移除和读取到它的父节点。
E.g。
public class CrabGroup extends HBox {
private static final Image c = new Image("File:crab.gif");
public CrabGroup() {
setSpacing(50);
}
/**
* Adds a crab
*/
public void addCrab() {
if(getChildren().size() < 6) {
ImageView image = new ImageView(c);
image.setFitWidth(100);
image.setFitHeight(100);
crabList.add(image);
}
}
/**
* Deletes a crab
*/
public void deleteCrab() {
if(!getChildren().isEmpty()) {
getChildren().remove(getChildren().size() - 1);
}
}
}
CrabGroup crabGroup = new CrabGroup();
crabGroup.setTranslateX(80);
crabGroup.setTranslateY(500);
crabGroup.addCrab();
...
crabGroup.deleteCrab();