我收到此错误:无法将java String类型的对象转换为com.example类型

时间:2018-10-29 03:25:21

标签: java android string

我已经编码12个小时了。直到出现此错误,我才编译我的代码。现在我无法解决。无法将对象转换为String。看来我必须转换这些位置,但是我已经在HomeFragment中进行转换。我仍然收到此错误。我在StackOverflow上进行了搜索,但是没有人的代码或建议无法解决该问题。

这是我的HomeFragment

public class HomeFragment extends Fragment {

private RecyclerView mUsersList;
private DatabaseReference mUserDatabase;
private View mView;
private FirebaseAuth mAuth;
ProgressDialog mProgressDialog;

public HomeFragment() {

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    mView = inflater.inflate(R.layout.fragment_home, container, false);

    mUsersList = (RecyclerView) mView.findViewById(R.id.users_list);
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new LinearLayoutManager(getActivity()));

    mProgressDialog = new ProgressDialog(getActivity());
    mProgressDialog.setTitle("İşlemler yapılıyor");
    mProgressDialog.setMessage("İşlemleriniz yapılırken lütfen bekleyiniz");
    mProgressDialog.setCanceledOnTouchOutside(true);

    mAuth = FirebaseAuth.getInstance();

    if (mAuth.getCurrentUser() != null) {

        String cUID = mAuth.getCurrentUser().getUid();

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Veriler").child(cUID);

        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String sFirma = dataSnapshot.child("sFirma").getValue(String.class);
                String sNot = dataSnapshot.child("sNot").getValue(String.class);
                String sServis = dataSnapshot.child("sServis").getValue(String.class);
                String sYakit = dataSnapshot.child("sYakit").getValue(String.class);
                String veri = dataSnapshot.child("veri").getValue(String.class);
                String zaman = dataSnapshot.child("zaman").getValue(String.class);
                String nFirma = dataSnapshot.child("nFirma").getValue(String.class);
                String nAlici_firma = dataSnapshot.child("nAlici_firma").getValue(String.class);
                String nArac = dataSnapshot.child("nArac").getValue(String.class);
                String nNot = dataSnapshot.child("nNot").getValue(String.class);
                String nYakit = dataSnapshot.child("nYakit").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };

        mUserDatabase.addListenerForSingleValueEvent(eventListener);
    }
    return mView;
}

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

    final FirebaseRecyclerAdapter<Users, UsersViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(Users.class, R.layout.user_single_layout, UsersViewHolder.class, mUserDatabase) {

        @Override
        protected void populateViewHolder(final UsersViewHolder viewHolder, final Users model, final int position) {

            viewHolder.setSfirma(model.getsFirma());
            viewHolder.setSservis(model.getsServis());
            viewHolder.setSyakit(model.getsYakit());
            viewHolder.setSnot(model.getsNot());
            viewHolder.setNfirma(model.getnFirma());
            viewHolder.setNalici_firma(model.getnAlici_firma());
            viewHolder.setNarac(model.getnArac());
            viewHolder.setNyakit(model.getsYakit());
            viewHolder.setNnot(model.getnNot());
            viewHolder.setVeri(model.getVeri());
            viewHolder.setZaman(model.getZaman());

            viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    new AlertDialog.Builder(getActivity())
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Veriyi silmek")
                            .setMessage("Bu veriyi silmek ister misiniz?")
                            .setPositiveButton("Evet", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    Log.i("position3", Integer.toString(position));

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

                                            mProgressDialog.show();


                                            try {

                                                mUsersList.removeViewAt(position);
                                                notifyItemRemoved(position);
                                                notifyDataSetChanged();

                                            } catch (Exception e) {

                                                e.printStackTrace();

                                            }
                                            //BURADA BİR ÖNCEKİ HATANDA İLK VERİ TABANINDAN SİLİP SONRA EKRANDAN SİLMEYE ÇALIŞTIN
                                            //BU DA SANA NULL POINTER HATASI DÖNDÜRDÜ UNUTMA!!!
                                            mUserDatabase.child(model.getZaman()).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {

                                                    if (task.isSuccessful()) {

                                                        mProgressDialog.dismiss();


                                                    } else {

                                                        Toast.makeText(getActivity(), "Silinirken hata oluştu", Toast.LENGTH_SHORT).show();

                                                    }
                                                }

                                            });

                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });

                                }
                            })
                            .setNegativeButton("Hayır", null)
                            .show();

                    return true;

                }
            });


        }
    };

    mUsersList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.notifyDataSetChanged();

}


public static class UsersViewHolder extends RecyclerView.ViewHolder {

    private View mView;

    public UsersViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

    }

    public void setSfirma(String sFirma) {

        TextView setFirma = mView.findViewById(R.id.user_firma_ismi);
        setFirma.setText("Firma İsmi : " + sFirma);
    }

    public void setSservis(String sServis) {

        TextView setServis = mView.findViewById(R.id.user_servis_adedi);
        setServis.setText("Servis Adedi : " + sServis);

    }

    public void setSyakit(String sYakit) {

        TextView setYakit = mView.findViewById(R.id.user_harcanan_yakit);
        setYakit.setText("Yakit : " + sYakit);

    }

    public void setSnot(String sNot) {

        TextView setNot = mView.findViewById(R.id.user_gunluk_not);
        setNot.setText("Not : " + sNot);

    }

    public void setNfirma(String nFirma) {

        TextView setNFirma = mView.findViewById(R.id.user_firma_ismi);
        setNFirma.setText("Firma İsmi : " + nFirma);

    }

    public void setNalici_firma(String nAlici_firma) {

        TextView setNAliciFirma = mView.findViewById(R.id.user_alici_firma_ismi);
        setNAliciFirma.setText("Alici Firma İsmi : " + nAlici_firma);

    }

    public void setNarac(String nArac) {

        TextView setNarac = mView.findViewById(R.id.user_servis_adedi);
        setNarac.setText("Nakliyat Adedi : " + nArac);

    }

    public void setNyakit(String nYakit) {

        TextView setNYakit = mView.findViewById(R.id.user_harcanan_yakit);
        setNYakit.setText("Harcanan Yakit : " + nYakit);

    }

    public void setNnot(String nNot) {

        TextView setNNot = mView.findViewById(R.id.user_gunluk_not);
        setNNot.setText("Firma İsmi : " + nNot);

    }

    public void setVeri(String veri) {

    }

    public void setZaman(String zaman) {


    }

}
}

这是我的Users.java课:

@IgnoreExtraProperties
public class Users {

public String sFirma;
public String sServis;
public String sYakit;
public String sNot;
public String nFirma;
public String nAlici_firma;
public String nArac;
public String nYakit;
public String nNot;
public String veri;
public String zaman;


public Users(String sFirma, String sServis, String sYakit, String sNot, String nFirma, String nAlici_firma, String nArac, String nYakit, String nNot, String veri, String zaman) {
    this.sFirma = sFirma;
    this.sServis = sServis;
    this.sYakit = sYakit;
    this.sNot = sNot;
    this.nFirma = nFirma;
    this.nAlici_firma = nAlici_firma;
    this.nArac = nArac;
    this.nYakit = nYakit;
    this.nNot = nNot;
    this.veri = veri;
    this.zaman = zaman;
}

public Users(){


}

@Exclude
public String getsFirma() {
    return sFirma;
}

public void setsFirma(String sFirma) {
    this.sFirma = sFirma;
}

@Exclude
public String getsServis() {
    return sServis;
}

public void setsServis(String sServis) {
    this.sServis = sServis;
}

@Exclude
public String getsYakit() {
    return sYakit;
}

public void setsYakit(String sYakit) {
    this.sYakit = sYakit;
}

@Exclude
public String getsNot() {
    return sNot;
}

public void setsNot(String sNot) {
    this.sNot = sNot;
}

@Exclude
public String getnFirma() {
    return nFirma;
}

public void setnFirma(String nFirma) {
    this.nFirma = nFirma;
}

@Exclude
public String getnAlici_firma() {
    return nAlici_firma;
}

public void setnAlici_firma(String nAlici_firma) {
    this.nAlici_firma = nAlici_firma;
}

@Exclude
public String getnArac() {
    return nArac;
}

public void setnArac(String nArac) {
    this.nArac = nArac;
}

@Exclude
public String getnYakit() {
    return nYakit;
}

public void setnYakit(String nYakit) {
    this.nYakit = nYakit;
}

@Exclude
public String getnNot() {
    return nNot;
}

public void setnNot(String nNot) {
    this.nNot = nNot;
}

@Exclude
public String getVeri() {
    return veri;
}

public void setVeri(String veri) {
    this.veri = veri;
}

@Exclude
public String getZaman() {
    return zaman;
}

public void setZaman(String zaman) {
    this.zaman = zaman;
}
}

错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.anild.servo, PID: 17495
              com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.anild.servo.Users
                  at com.google.android.gms.internal.zg.zzb(Unknown Source)
                  at com.google.android.gms.internal.zg.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
                  at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:141)
                  at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:127)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:164)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
                  at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
                  at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                  at android.view.Choreographer.doFrame(Choreographer.java:603)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5525)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

enter image description here

2 个答案:

答案 0 :(得分:0)

 if (mAuth.getCurrentUser() != null) {

    String cUID = mAuth.getCurrentUser().getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Veriler").child(cUID);

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Users muser = dataSnapshot.child("sFirma").getValue(Users.class);
            Users mnewuser = new User();
            String sFirma = muser.getsFirma();
            String sServis= muser.getsServis();
            String sYakit= muser.getsYakit();
            String sNot= muser.getsNot();
            String nFirma= muser.getnFirma();
            String nAlici_firma= muser.getnAlici_firma();
            String nArac= muser.getnArac();
            String nYakit= muser.getnYakit();
            String nNot= muser.getnNot();
            String veri= muser.getveri();
            String zaman= muser.getzaman();
            mnewuser.setsFirma(sFirma);
         //set rest of your string in model as above
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    mUserDatabase.addListenerForSingleValueEvent(eventListener);

}

答案 1 :(得分:0)

尝试如下更改您的onDataChange方法:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
            String sFirma = dataSnapshot.child("sFirma").getValue(Users.class);
            String sNot = dataSnapshot.child("sNot").getValue(Users.class);
            String sServis = dataSnapshot.child("sServis").getValue(Users.class);
            String sYakit = dataSnapshot.child("sYakit").getValue(Users.class);
            String veri = dataSnapshot.child("veri").getValue(Users.class);
            String zaman = dataSnapshot.child("zaman").getValue(Users.class);
            String nFirma = dataSnapshot.child("nFirma").getValue(Users.class);
            String nAlici_firma = dataSnapshot.child("nAlici_firma").getValue(Users.class);
            String nArac = dataSnapshot.child("nArac").getValue(Users.class);
            String nNot = dataSnapshot.child("nNot").getValue(Users.class);
            String nYakit = dataSnapshot.child("nYakit").getValue(Users.class);
}