错误:预期')'

时间:2017-04-15 13:31:54

标签: android

我收到的错误是“)”。我很确定这是因为它期待'});'当我在这行'databaseClients.addValueEventListener (new ValueEventListener() {'上打开一个括号时。但是,当我添加/修改它时,该程序仍然抱怨。不知道为什么。

这很可能是一个非常愚蠢的错误! 任何帮助表示赞赏。干杯啦!

public class ViewProfile extends AppCompatActivity{

    ListView listviewClients;
    List<Clients> clientsList;

    DatabaseReference databaseClients;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_profile);

        listviewClients = (ListView) findViewById(R.id.listviewClients);
        databaseClients = FirebaseDatabase.getInstance().getReference("clients");

        clientsList = new ArrayList<>();
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseClients.addValueEventListener (new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                clientsList.clear();

                for (DataSnapshot clientsSnapshot : dataSnapshot.getChildren()) {
                    Clients clients = clientsSnapshot.getValue(Clients.class);

                    clientsList.add(clients);
                }
                ClientsList adapter = new ClientsList(ViewProfile.this, clientsList);
                listviewClients.setAdapter(adapter);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }

        }
    }
}

修改 当我添加});有时候是这样的: enter image description here

我现在得到的错误是:错误:(42,48)错误:不兼容的类型:无法转换为com.google.firebase.database.ValueEventListener。

2 个答案:

答案 0 :(得分:1)

public class ViewProfile extends AppCompatActivity{

ListView listviewClients;
List<Clients> clientsList;

DatabaseReference databaseClients;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_profile);

    listviewClients = (ListView) findViewById(R.id.listviewClients);
    databaseClients = FirebaseDatabase.getInstance().getReference("clients");

    clientsList = new ArrayList<>();
}

@Override
protected void onStart() {
    super.onStart();

    databaseClients.addValueEventListener (new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            clientsList.clear();

            for (DataSnapshot clientsSnapshot : dataSnapshot.getChildren()) {
                Clients clients = clientsSnapshot.getValue(Clients.class);

                clientsList.add(clients);
            }
            ClientsList adapter = new ClientsList(ViewProfile.this, clientsList);
            listviewClients.setAdapter(adapter);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }

    });  //here is your problem
}

}

答案 1 :(得分:1)

这可能是您的导入问题。

您必须使用com.google.firebase.database.ValueEventListener。但我认为您在导入文件的顶部有com.firebase.client.ValueEventListener导入。

Android Firebase: Can´t add ValueEventListener