Android:将数据上传到Firebase后,应用程序崩溃了

时间:2018-02-13 11:48:52

标签: android firebase firebase-realtime-database

有2个Edittext框和1个按钮。点击按钮后,数据上传到firebase,但应用程序崩溃了。

我甚至不知道出了什么问题。一切看起来都很正确。当只有一个EditText时,一切正常。但在添加另一个应用程序后,该应用程序正在崩溃。这是代码:

public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
        this.localView = paramLayoutInflater.inflate(R.layout.fragmentupload, paramViewGroup, false);
    t1 = (TextView) localView.findViewById(R.id.submit_title);
    userstoryText = (EditText) localView.findViewById(R.id.user_story);
    usernameText = (EditText) localView.findViewById(R.id.user_name);
    submitbtn = (Button) localView.findViewById(R.id.submit_story);
    rl = (RelativeLayout) localView.findViewById(R.id.upload_layout);
    databaseReference = FirebaseDatabase.getInstance().getReference();
    submitbtn.setOnClickListener(this);
    return localView;

}

public void onClick(View v)
{
    if(userstoryText.getText().toString().isEmpty())
    {
        Toast.makeText(getActivity(),"the storybox is empty", Toast.LENGTH_SHORT).show();
        return;
    } else
    {
        AddData();
    }

}

public void AddData()
{
    String Story = userstoryText.getText().toString().trim();
    String Uname = usernameText.getText().toString().trim();

    HashMap<String, String> dataMap = new HashMap<String, String>();
    dataMap.put("username", Uname);
    dataMap.put("storytext" , Story);

    databaseReference.push().setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if (task.isSuccessful())
            {
                userstoryText.setText("");
                usernameText.setText("");
                Toast.makeText(getActivity(),"Submitted Successfully",Toast.LENGTH_SHORT).show();

            }
        }
    });
}

这是错误日志

    05-23 15:13:54.651 1314-1348/? E/BufferQueueProducer: [InputMethod] cancelBuffer: slot 1 is not owned by the producer (state = ACQUIRED)
05-23 15:14:04.780 1314-1348/? E/BufferQueueProducer: [InputMethod] cancelBuffer: slot 1 is not owned by the producer (state = ACQUIRED)
05-23 15:14:35.889 12168-12168/com.dcreativity.scribbledstories E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                                         at android.view.LayoutInflater.from(LayoutInflater.java:232)
                                                                                         at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
                                                                                         at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166)
                                                                                         at com.dcreativity.scribbledstories.DataDisplayAdapter.<init>(DataDisplayAdapter.java:26)
                                                                                         at com.dcreativity.scribbledstories.FragmentHome$1.onDataChange(FragmentHome.java:77)
                                                                                         at com.google.android.gms.internal.to.zza(Unknown Source)
                                                                                         at com.google.android.gms.internal.vj.zzHX(Unknown Source)
                                                                                         at com.google.android.gms.internal.vp.run(Unknown Source)
                                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


                                                                                     --------- beginning of crash
05-23 15:14:35.889 12168-12168/com.dcreativity.scribbledstories E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.dcreativity.scribbledstories, PID: 12168
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                                      at android.view.LayoutInflater.from(LayoutInflater.java:232)
                                                                                      at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
                                                                                      at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166)
                                                                                      at com.dcreativity.scribbledstories.DataDisplayAdapter.<init>(DataDisplayAdapter.java:26)
                                                                                      at com.dcreativity.scribbledstories.FragmentHome$1.onDataChange(FragmentHome.java:77)
                                                                                      at com.google.android.gms.internal.to.zza(Unknown Source)
                                                                                      at com.google.android.gms.internal.vj.zzHX(Unknown Source)
                                                                                      at com.google.android.gms.internal.vp.run(Unknown Source)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-23 15:14:37.443 2305-2387/com.google.android.googlequicksearchbox:search E/AudioRecord: Could not get audio input for session 3521, record source 1999, sample rate 16000, format 0x1, channel mask 0x10, flags 0
05-23 15:14:37.455 2305-2387/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.
05-23 15:14:37.455 2305-2387/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-23 15:14:37.455 2305-2387/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
05-23 15:14:42.523 2305-2387/com.google.android.googlequicksearchbox:search E/AudioRecord: Could not get audio input for session 3529, record source 1999, sample rate 16000, format 0x1, channel mask 0x10, flags 0
05-23 15:14:42.523 2305-2387/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.

这两行在错误日志中以蓝色下划线突出显示。一个正在扩展的片段。 enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您正在程序中传递两个编辑文本参数,因此请尝试为 usernameText 添加else if条件。

if(userstoryText.getText().toString().isEmpty())
    {
        Toast.makeText(getActivity(),"the storybox is empty", Toast.LENGTH_SHORT).show();
        return;
    } 
 else if(usernameText.getText().toString().isEmpty())
    {
        Toast.makeText(getActivity(),"the usernamebox is empty", Toast.LENGTH_SHORT).show();
        return;
    } 

 else
    {
        AddData();
    }

如果没有工作,请添加问题的错误日志

答案 1 :(得分:0)

要解决此问题,请更改以下代码行:

Toast.makeText(getActivity(),"the storybox is empty", Toast.LENGTH_SHORT).show();

Toast.makeText(getContext(),"the storybox is empty", Toast.LENGTH_SHORT).show();