为什么我的第二项应用活动崩溃了?

时间:2018-08-08 17:49:22

标签: android firebase firebase-realtime-database

[它显示logcat详细信息。其为空] [1]连接Firebase数据库后,我的应用程序崩溃了。基于我在教程中看到的知识,如何将Firebase数据库添加到应用程序。我已连接我的应用程序,但不确定如何使应用程序崩溃以及我的应用程序是否与数据库链接。我在这里的三个和第二个活动中有三个活动需要将用户详细信息发送到数据库。但是现在第二项活动已经崩溃,我不知道第三项。 甚至我的logcat也没有显示任何详细信息。 这是我的第二个活动Java文件:

package com.example.admin.howdy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 01-Aug-18.
 */

public class second extends Activity{

    Spinner institute;
    RadioGroup type;
    DatabaseReference databaseReference;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        databaseReference= FirebaseDatabase.getInstance().getReference("Feedbacks");
        Spinner sp=(Spinner)findViewById(R.id.spinner);
        List<String> categories = new ArrayList<String>();
        categories.add("Prince Dr K Vasudevan College of Engineering and Technology");
        categories.add("Prince Shri Venkateshwara Padmavati College of Engineering");
        categories.add("Prince Maticulation Higher Secondry School");
        categories.add("Prince Shri Venkateshwara College of Arts and Science");
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(second.this,android.R.layout.simple_spinner_item,categories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                sendUserType();
                Intent myintent2 = new Intent(second.this, third.class);
                startActivity(myintent2);
            }
        });
    }

   private void sendUserType()
{
    String usertypevar2="";
    String userinstitue=institute.getSelectedItem().toString();
    type = (RadioGroup) findViewById(R.id.radioGroup);
    int id=type.getCheckedRadioButtonId();
    if (id != -1) {
        RadioButton ex=(RadioButton)findViewById(id);
        usertypevar2=ex.getText().toString();
    }
    else{
        Toast.makeText(this,"Please Select An Option", Toast.LENGTH_LONG).show();
    }
    RadioButton ex=(RadioButton)findViewById(id);
    usertype up=new usertype(usertypevar2,userinstitue);
     databaseReference.setValue(up).addOnSuccessListener(new OnSuccessListener<Void>() {
        public void onSuccess(Void aVoid) {
            System.out.print("Successful");
        }}).addOnFailureListener(new OnFailureListener()
    { public void onFailure(@NonNull Exception e) {
        System.out.print("not Successful");
    } });;
    }
}

这是我的第二个活动xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginStart="13dp"
        android:layout_marginTop="37dp"
        android:fontFamily="@font/aladin"
        android:text="Complete following steps to proceed further."
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="108dp"
        android:layout_height="29dp"
        android:layout_marginBottom="13dp"
        android:layout_marginStart="25dp"
        android:fontFamily="@font/aladin"
        android:text="Who are you?"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/radioGroup"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="154dp"
        android:layout_height="114dp"
        android:layout_marginBottom="13dp"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView3">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_weight="1"
            android:buttonTint="@color/colorPrimaryDark"
            android:fontFamily="@font/amita"
            android:text="Students"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_weight="1"
            android:buttonTint="@color/colorPrimaryDark"
            android:fontFamily="@font/amita"
            android:text="Staffs"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_weight="1"
            android:buttonTint="@color/colorPrimaryDark"
            android:fontFamily="@font/amita"
            android:text="Others"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="34dp"
        android:layout_marginBottom="9dp"
        android:layout_marginLeft="24dp"
        android:fontFamily="@font/aladin"
        android:text="Which instution you belongs to?"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/spinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="249dp"
        android:layout_height="29dp"
        android:layout_marginBottom="68dp"
        android:layout_marginLeft="24dp"
        android:prompt="@string/app_name"
        android:spinnerMode="dialog"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="56dp"
        android:layout_marginStart="137dp"
        android:background="?android:attr/colorEdgeEffect"
        android:text="Next"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_editor_absoluteX="137dp" />

</android.support.constraint.ConstraintLayout>
这是我的Java类用户类型:

package com.example.admin.howdy;

/**
 * Created by admin on 08-Aug-18.
 */

public class usertype {
    private String usertypevar;
    private String userinstitute;

    public usertype()
    {

    }
    public usertype(String usertypevar,String userinstitute)
    {
        this.usertypevar=usertypevar;
        this.userinstitute=userinstitute;
    }
    public String getUsertype() {
        return usertypevar;
    }

    public String getUserinstitute() {
        return userinstitute;
    }
}
我已经从logcat窗口附加了一些错误消息:

`08-11 18:28:56.771 19397-19397/com.example.admin.howdy E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.admin.howdy, PID: 19397
                                                                     java.lang.NoSuchMethodError: No virtual method zzEq()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.admin.howdy-1/split_lib_dependencies_apk.apk)
                                                                         at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                         at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                         at com.example.admin.howdy.second.onCreate(second.java:36)
                                                                         at android.app.Activity.performCreate(Activity.java:5990)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)`

2 个答案:

答案 0 :(得分:1)

您尚未初始化广播组,即键入。

这样做:-

type = (RadioGroup) findViewById(R.id.radioGroup);

之前

int id=type.getCheckedRadioButtonId();

更新:-

int id=type.getCheckedRadioButtonId();
RadioButton ex=(RadioButton)findViewById(id);

这些行也可能导致崩溃。当用户按下按钮而不选择任何单选按钮时,方法getCheckedRadioButtonId()返回-1,当您使用id = -1的findViewById查找单选按钮时,将导致出现空指针异常,从而导致应用程序崩溃。

因此,解决此问题的方法是您可以添加检查以检测id是否为-1:-

int id=type.getCheckedRadioButtonId();

// If nothing is selected from Radio Group, then it return -1
if (id != -1) {
    RadioButton ex=(RadioButton)findViewById(id);
    String usertypevar2=ex.getText().toString();
}
else{
    Toast.makeText(this,"Please Select An Option", Toast.LENGTH_LONG).show();
}

答案 1 :(得分:1)

typenull中是sendUserType()。因此,下面的代码将引发NPE。

int id=type.getCheckedRadioButtonId();