0

I want to make a comment page like Instagram. I want to make a fragment where users using the application can add comments. Comments are kept in firebase. With the codes I wrote, the relevant section does not occur in firebase, and comments are not saved.

Yorumyaz2Fragment.java:

public class Yorumyaz2Fragment extends Fragment {
    EditText edit_yorumekle;
    TextView gonder1;
    ImageView image;

    String postid;
    String publisherid;
    FirebaseUser user;


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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_yorumyaz2, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar1);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Yorumlar");
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(v -> getActivity().finish());
        edit_yorumekle=(EditText) view.findViewById(R.id.edit_yorumekle);
        gonder1=(TextView) view.findViewById(R.id.gonder1);
        image=(ImageView) view.findViewById(R.id.image);
        user= FirebaseAuth.getInstance().getCurrentUser();

        Intent intent=getActivity().getIntent();
        postid=intent.getExtras().getString("postid");
        publisherid=intent.getExtras().getString("publisherid");


        gonder1.setOnClickListener(v -> {
            if(edit_yorumekle.getText().toString().equals("")){
                Toast.makeText(getContext(),"boş yorum gönderemezsiniz",Toast.LENGTH_LONG).show();

            }
            else {
                yorumeklee();
            }
        });


        return  view;

    }

    private void yorumeklee() {
     if(postid !=null){

            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("yorumlar").child(postid);     
            HashMap<String,Object> hash=new HashMap<>();
            hash.put("yorum",edit_yorumekle.getText().toString());
            hash.put("gönderen",user.getUid());
            reference.push().setValue(hash);
            edit_yorumekle.setText("");
    }
}

fragment_yorumyaz2.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Yorumyaz2Fragment">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar1"
        android:background="?android:attr/windowBackground">
    </androidx.appcompat.widget.Toolbar>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler1"
        android:layout_below="@+id/toolbar1"
        android:layout_above="@+id/bottom"></androidx.recyclerview.widget.RecyclerView>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom"
        android:background="#fff"
        android:padding="4dp"
        android:layout_alignParentBottom="true">

        <com.mikhaellopez.circularimageview.CircularImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/image">
        </com.mikhaellopez.circularimageview.CircularImageView>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/image"
            android:hint="Yorumunuzu giriniz"
            android:id="@+id/edit_yorumekle"
            android:layout_centerVertical="true"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:layout_toLeftOf="@+id/gonder"
            android:background="@android:color/transparent">
        </EditText>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gönder"
            android:layout_marginEnd="5dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:textColor="@color/design_default_color_primary"
            android:id="@+id/gonder1"
            android:layout_marginRight="5dp"
            android:layout_alignParentRight="true"></TextView>
    </RelativeLayout>


</RelativeLayout>

Logcat:

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
        at com.example.geziproject.Yorumyaz2Fragment.onCreateView(Yorumyaz2Fragment.java:66)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

2 Answers 2

2

The main problem you're facing is that postId is null so when you call .child(postId) it's throwing an exception.

Now, postId is null because you aren't getting the extra value in the correct way.

Change this:

Intent intent=new Intent();
postid=intent.getStringExtra("postid");
publisherid=intent.getStringExtra("publisherid");

To this:

Intent intent = getActivity().getIntent();
postid = intent.getExtras().getString("postid");
publisherid = intent.getExtras().getString("publisherid");

And I'd suggest that you check postId is not null before you call yorumeklee() to avoid getting the exception you showed.

3
  • Please @KübraOKUR if this answer solved your problem, consider marking it as the "accepted answer".
    – Brugui
    Jan 5, 2021 at 15:18
  • As you said, I tried to edit my code, but I got another error in logcat. Would you look again Jan 5, 2021 at 17:12
  • I guess that you aren sending the data to the fragment in the wrong way, please check this answer
    – Brugui
    Jan 5, 2021 at 17:20
1

The problem is in

Intent intent=new Intent();
postid=intent.getStringExtra("postid");
publisherid=intent.getStringExtra("publisherid");

part of the code. U create new Intent with Intent intent=new Intent(); and then acces parameters of this intent. That's why you have value of postid null. You should pass the data to the fragment like this: Send data from activity to fragment in Android

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.