无法启动涉及Firebase的新活动

时间:2017-07-01 21:15:31

标签: android

我正在构建一个拥有慈善机构和餐厅的应用程序。他们注册自己,然后继续使用Firebase进行数据库处理。当我注册慈善机构注册慈善机构应该打开,但它显示错误。 Logcat并注册附加的慈善文件。

活动注册慈善机构

package com.example.aayu.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

/**
 * Created by AAYU on 30-May-17.
 */

public class regc extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reg_c);




    final DatabaseReference rootref = FirebaseDatabase.getInstance()
            .getReferenceFromUrl("https://myapplication-6e0b9.firebaseio.com/charity");

    final EditText mname = (EditText) findViewById(R.id.cname);
    final EditText madd = (EditText) findViewById(R.id.cadd);
    final EditText mcity = (EditText) findViewById(R.id.ccity);
    final EditText mstate = (EditText) findViewById(R.id.cstate);
    final EditText mpin = (EditText) findViewById(R.id.cpin);



    final Button mnext= (Button) findViewById(R.id.cnext);

      mnext.setOnClickListener(new View.OnClickListener(){

          @Override
          public void onClick(View view){
              String name = mname.getText().toString();
              String add = madd.getText().toString();
              String city = mcity.getText().toString();
              String state = mstate.getText().toString();
              String pin = mpin.getText().toString();

              DatabaseReference childref = rootref.child("Charity Name");
              childref.setValue(name);
              childref = rootref.child("Address");
              childref.setValue(add);
              childref = rootref.child("City");
              childref.setValue(city);
              childref = rootref.child("State");
              childref.setValue(state);
              childref = rootref.child("Pincode");
              childref.setValue(pin);
          }
      });



    //setContentView(R.layout.reg2_c);

    final EditText memail = (EditText) findViewById(R.id.cemail);
    final EditText mph = (EditText) findViewById(R.id.cph);
    final EditText muname = (EditText) findViewById(R.id.cuname);
    final EditText  mpwd = (EditText) findViewById(R.id.cpwd);


    final Button mreg=(Button) findViewById(R.id.creg);

    mreg.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {

            String email = memail.getText().toString();
            String ph = mph.getText().toString();
            String uname = muname.getText().toString();
            String pwd = mpwd.getText().toString();

            DatabaseReference childref = rootref.child("Email Id");
            childref.setValue(email);
            childref = rootref.child("Phone No");
            childref.setValue(ph);
            childref = rootref.child("Username");
            childref.setValue(uname);
            childref = rootref.child("Password");
            childref.setValue(pwd);

            Toast.makeText(view.getContext(), "Registration done successfully", Toast.LENGTH_SHORT).show();
        }
        });


       // Intent loginc = new Intent(this, loginc.class);
       // startActivity(loginc);


}
}

的logcat

W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11011000 but found 9877470
V/FA: Activity paused, time: 32230643
V/FA: Processing queued up service tasks: 2
E/FA: Failed to send current screen to service
E/FA: Discarding data. Failed to send event to service
D/AndroidRuntime: Shutting down VM
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aayu.myapplication/com.example.aayu.myapplication.regc}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:154)
                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                     at com.example.aayu.myapplication.regc.onCreate(regc.java:76)
                     at android.app.Activity.performCreate(Activity.java:6662)
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.aayu.myapplication, PID: 9023
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aayu.myapplication/com.example.aayu.myapplication.regc}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at com.example.aayu.myapplication.regc.onCreate(regc.java:76)
                  at android.app.Activity.performCreate(Activity.java:6662)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
Application terminated.

reg_c.xml

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

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginStart="51dp"
    android:layout_marginTop="49dp"
    android:id="@+id/cname"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:hint="Charity Home Name"
    android:selectAllOnFocus="true" />

<Button
    android:text="NEXT"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cnext"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:background="@color/colorPrimary"
    android:textStyle="normal|bold"
    android:textSize="20sp"
    android:elevation="21dp"
    android:onClick="next" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginTop="24dp"
    android:id="@+id/cadd"
    android:layout_below="@+id/cname"
    android:layout_alignStart="@+id/cname"
    android:hint="Address "
    android:selectAllOnFocus="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginTop="36dp"
    android:id="@+id/ccity"
    android:hint="City"
    android:selectAllOnFocus="true"
    android:layout_below="@+id/cadd"
    android:layout_alignStart="@+id/cadd" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginTop="48dp"
    android:id="@+id/cstate"
    android:hint="State"
    android:selectAllOnFocus="true"
    android:layout_below="@+id/ccity"
    android:layout_alignStart="@+id/ccity" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_marginTop="34dp"
    android:id="@+id/cpin"
    android:hint="Pincode"
    android:selectAllOnFocus="true"
    android:layout_below="@+id/cstate"
    android:layout_alignStart="@+id/cstate" />

</RelativeLayout>

reg_c.xml的布局

layout for reg_c.xml

1 个答案:

答案 0 :(得分:0)

问题在于:

final Button mreg=(Button) findViewById(R.id.creg);

mreg.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view) {

        String email = memail.getText().toString();
        String ph = mph.getText().toString();
        String uname = muname.getText().toString();
        String pwd = mpwd.getText().toString();

        DatabaseReference childref = rootref.child("Email Id");
        childref.setValue(email);
        childref = rootref.child("Phone No");
        childref.setValue(ph);
        childref = rootref.child("Username");
        childref.setValue(uname);
        childref = rootref.child("Password");
        childref.setValue(pwd);

        Toast.makeText(view.getContext(), "Registration done successfully", Toast.LENGTH_SHORT).show();
    }
    });

您的XML文件中没有任何带有此ID的按钮,但您尝试在Activity中访问它。这导致空指针异常。

你有两个解决方案:

  1. 如果您需要此按钮,请在XML文件中添加ID为“creg”的按钮。

  2. 如果您不需要该按钮,请从您的活动中删除上面给出的代码。