我想做一个像instagram这样的评论页面。我想制作一个片段,使用该应用程序的用户可以在其中添加注解。评论保存在firebase中。使用我编写的代码,相关部分不会出现在firebase中,注解也不会保存。
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("");
}
}
片段\u 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>
日志:
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条答案
按热度按时间weylhg0b1#
问题出在
代码的一部分。创造新的意图
Intent intent=new Intent();
然后访问此意图的参数。所以你才有价值postid
无效的。您应该像这样将数据传递到片段:在android中将数据从活动发送到片段vd8tlhqk2#
你面临的主要问题是
postId
是空的所以当你打电话的时候.child(postId)
它引发了一个异常。现在,
postId
为null,因为您没有以正确的方式获取额外的值。更改此项:
对此:
我建议你检查一下
postId
在调用之前不为nullyorumeklee()
以避免出现您显示的异常。