2017-07-03 25 views
-2

這是我的logcat。java.lang.NullPointerException:活動不能爲空?

07-03 17:17:01.085 24875-24875/com.example.faisal.fuel_share E/Trace: error opening trace file: No such file or directory (2) 

07-03 17:17:01.165 24875-24921/com.example.faisal.fuel_share E/dalvikvm: 

Could not find class 'android.app.AppOpsManager', referenced from method 
com.google.android.gms.internal.zzacw.zzg 

07-03 17:17:18.901 24875-24875/com.example.faisal.fuel_share E/AndroidRuntime: FATAL EXCEPTION: main 

java.lang.NullPointerException: Activity must not be null 
                       at com.google.android.gms.common.internal.zzac.zzb(Unknown Source) 
                       at com.google.android.gms.internal.zzaav.<init>(Unknown Source) 
                       at com.google.android.gms.internal.zzaaw.zzs(Unknown Source) 
                       at com.google.android.gms.tasks.zzh$zza.zzw(Unknown Source) 
                       at com.google.android.gms.tasks.zzh.addOnCompleteListener(Unknown Source) 
                       at com.example.faisal.fuel_share.FYP_DAO.GetHelpDB.singup(GetHelpDB.java:68) 
                       at com.example.faisal.fuel_share.FYP_DAO.GetHelpDB.<init>(GetHelpDB.java:35) 
                       at com.example.faisal.fuel_share.GetHelp.singUp(GetHelp.java:82) 
                       at com.example.faisal.fuel_share.GetHelp.access$000(GetHelp.java:18) 
                       at com.example.faisal.fuel_share.GetHelp$1.onClick(GetHelp.java:42) 
                       at android.view.View.performClick(View.java:4209) 
                       at android.view.View$PerformClick.run(View.java:17360) 
                       at android.os.Handler.handleCallback(Handler.java:725) 
                       at android.os.Handler.dispatchMessage(Handler.java:92) 
                       at android.os.Looper.loop(Looper.java:137) 
                       at android.app.ActivityThread.main(ActivityThread.java:5041) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:511) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                       at dalvik.system.NativeStart.main(Native Method) 

這是我的代碼..

public class GetHelp extends Activity { 
    private EditText emailid, nameid, passwordid, phonenoid; 

    /*protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(this); 
    } 
    */ 



    private TextInputLayout inputlayoutname, inputlayoutemail, 
    inputlayoutpassword, inputlayoutphoneno; 
    Button submit; 
    GetHelpDB ghDb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_gethelp); 
    initComponents(); 
    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      singUp(); 

     } 
    }); 
} 

private void singUp() { 
    boolean isValid = true; 
    if (nameid.getText().toString().isEmpty()) { 
     inputlayoutname.setError("Name is Mandatory"); 
     isValid = false; 
    } else { 
     inputlayoutname.setErrorEnabled(false); 
    } 
    if (emailid.getText().toString().isEmpty()) { 
     inputlayoutemail.setError("Email is Mandatory"); 
     isValid = false; 
    } else { 
     inputlayoutemail.setErrorEnabled(false); 
    } 
    if (passwordid.getText().toString().trim().length() < 8) { 
     inputlayoutpassword.setError("Minimum 8 Character Required"); 
     isValid = false; 
    } else { 
     inputlayoutpassword.setErrorEnabled(false); 
    } 

    if (phonenoid.getText().toString().isEmpty()) { 
     inputlayoutphoneno.setError("Phone Number is Mandatory"); 
     isValid = false; 
    } else { 
     inputlayoutphoneno.setErrorEnabled(false); 
    } 
    if (isValid) { 
     Toast.makeText(GetHelp.this, "welcome", Toast.LENGTH_SHORT).show(); 
     GetHelpBean gb=new GetHelpBean(); 
     gb.setNameid(nameid.getText().toString()); 
     gb.setEmailid(emailid.getText().toString()); 
     gb.setPasswordid(passwordid.getText().toString()); 
     gb.setPhonenoid(phonenoid.getText().toString()); 
     ghDb=new GetHelpDB(gb); 
     /* Intent i=new Intent(this, GetHelpProfile.class); 
     i.putExtra("GetHelp", gb); 
     startActivity(i); */ 





    } 

} 

private void initComponents() { 
    nameid = (EditText) findViewById(R.id.nameid); 
    emailid = (EditText) findViewById(R.id.emailid); 
    passwordid = (EditText) findViewById(R.id.passwordid); 
    phonenoid = (EditText) findViewById(R.id.phonenoid); 
    inputlayoutname = (TextInputLayout) findViewById(R.id.inputlayoutname); 
    inputlayoutemail = (TextInputLayout) findViewById(R.id.inputlayoutemail); 
    inputlayoutpassword = (TextInputLayout) findViewById(R.id.inputlayoutpassword); 
    inputlayoutphoneno = (TextInputLayout) findViewById(R.id.inputlayoutphoneno); 
    submit = (Button) findViewById(R.id.submit); 
    } 
} 
+0

這可能有助於https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix-it。 stackoverflow.com/questions/... – seon

回答

1

您還沒有聲明和初始化context.Declare它作爲Context context;並在onCreate方法來初始化它context=GetHelp.this;。 這是造成你的NPE。

相關問題