需要将dataSnapshot声明为final

时间:2019-02-02 12:45:30

标签: android firebase firebase-realtime-database

我在课堂上使用变量,但出现错误:

  

从内部类内部访问变量dataSnapshot,需要将其声明为最终变量

我知道如果我在onCreateView内部使用,则需要使用final,但我不使用。那为什么我会收到此错误?

我仅在setOnClickListener事件中收到此错误。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.util.Log;


import com.google.firebase.auth.FirebaseAuth;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.auth.FirebaseUser;

import java.util.Locale;

public class Show extends Fragment {



    //private FirebaseAuth user;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();



    String credit;
    String current;
    String values;
    String odds;


    TextView tv1odds;
    TextView tv1values;
    TextView currentcredit;

    LinearLayout tv1kilit;



    public Show() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View Gview = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);




         tv1kilit = (LinearLayout)Gview.findViewById(R.id.Tvalue1kilit);




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

                        if (dataSnapshot.child("tutka").getValue().equals("Yes")) {
                        Log.d("ERR", "OK");

                        }else{
                        Log.d("ERR", "NOPE");

                        }



                            current = dataSnapshot.child("Users").child(user.getUid()).child("credit").getValue().toString();
                            currentcredit.setText(getResources().getString(R.string.credit) + " " + current);




                            tv1kilit.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    int eksilt = Integer.parseInt(credit); 
                                    int kullanıcıBakiyesi = Integer.parseInt(dataSnapshot.child("Users").child(user.getUid()).child("credit").getValue().toString());
                                    int kalan = kullanıcıBakiyesi - eksilt;

                                    if (kalan >= 0 && tv1values.getText().subSequence(0, 2).equals("Cr") && !Locale.getDefault().getDisplayLanguage().equals("Türkçe") && !dataSnapshot.child("Users").child(user.getUid()).child("Tvalue1").getValue().equals(dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString())) {
                                        myRef.child("Users").child(user.getUid()).child("credit").setValue(Integer.toString(kalan)); //kullanıcı creditsi
                                        tv1values.setText(values);
                                        tv1odds.setText(odds);
                                        myRef.child("Users").child(user.getUid()).child("Tvalue1").setValue(dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString());
                                        String koy = dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString();
                                        myRef.child("Users").child(user.getUid()).child("Tvalue1").setValue(koy);
                                    } else if (kalan >= 0 && tv1values.getText().subSequence(0, 2).equals("Kr") && Locale.getDefault().getDisplayLanguage().equals("Türkçe") && !dataSnapshot.child("Users").child(user.getUid()).child("Tvalue1").getValue().equals(dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString())) {
                                        myRef.child("Users").child(user.getUid()).child("credit").setValue(Integer.toString(kalan)); //kullanıcı creditsi
                                        tv1values.setText(values);
                                        tv1odds.setText(odds);
                                        myRef.child("Users").child(user.getUid()).child("Tvalue1").setValue(dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString());
                                        String koy = dataSnapshot.child("Tvalue1").child("Oynayanlar").getValue().toString();
                                        myRef.child("Users").child(user.getUid()).child("Tvalue1").setValue(koy);

                                    } else if (kalan < 0) {
                                        //Toast.makeText(getApplicationContext(), getResources().getString(R.string.errenoughtcredit), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });









                }
                @Override
                public void onCancelled(DatabaseError error) {
                }
            });

        } catch (Exception e) {

            //Toast.makeText(this.getActivity(), R.string.errenoughtcredit, Toast.LENGTH_LONG).show();
        }





        return Gview;
    }

}

1 个答案:

答案 0 :(得分:1)

您正在使用

dataSnapshot 

内部方法

onClick 

创建的匿名内部类中
new View.OnClickListener()  

在匿名内部类中使用的任何局部变量都必须声明为final,因为编译器会在该类中创建一个字段来保存对其的引用。