2017-07-01 57 views
-2

我正在构建一个有慈善机构和餐厅的应用。他们自行注册,然后继续使用Firebase进行数据库处理。当我注册慈善注册慈善机构应该打开,但它显示错误。 Logcat并注册慈善文件。无法启动涉及Firebase的新活动

活动寄存器慈善

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

+0

你可以在这里发布“reg_c.xml”文件吗?问题在于你的Activity无法从布局文件中找到按钮。请在这里发布上面提到的文件。所以我可以帮你找到问题。 –

+0

请阅读[在什么情况下,我可以添加“紧急”或其他类似的短语到我的问题,以获得更快的答案?](/ meta.stackoverflow.com/q/326569) - 总结是,这不是这是解决志愿者问题的理想方式,可能会对获得答案产生反作用。请不要将这添加到您的问题。 – halfer

+0

@ZohaibHassan添加文件。感谢您的帮助。 –

回答

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(); 
    } 
    }); 

您没有任何按钮,此ID在你的XML文件,但您要访问它在你的活动。这导致空指针异常。

你有两个解决方案:

  1. 如果你需要这个按钮,然后在你的XML文件中的ID“CREG”添加一个按钮。

    OR

  2. 如果您不需要这个按钮,然后从你的活动上面给出的代码。