2016-02-29 59 views
0

我正在開發一個Android應用程序,我有一些問題。EditText.GetText,返回關於null引用的異常,但它被聲明爲

我有一些EditText輸入,我想用輸入文本創建一個名爲Contest的對象。我的代碼如下:

package com.example.torge.projectnow.loginSystem; 

import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 

import com.example.torge.projectnow.R; 

public class AddContest extends AppCompatActivity implements View.OnClickListener{ 

    Spinner sCategoryC = null; 
    String idUserUpload = "1"; 
    EditText etContestName, etShorDesC, etDeadlineC, etAgeLimitC, etStartDateC, etLocatoinC, etFullDesC, etCostC; 
    UserLocalStore userLocalStore; 
    Button bSendContest; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_contest); 
     sCategoryC = (Spinner) findViewById(R.id.sCategoryContest); 
     etContestName = (EditText) findViewById(R.id.etContestName); 
     etShorDesC = (EditText) findViewById(R.id.etShortDesC); 
     etDeadlineC = (EditText) findViewById(R.id.etDeadlineC); 
     etAgeLimitC = (EditText) findViewById(R.id.etAgeLimitC); 
     etStartDateC = (EditText) findViewById(R.id.etStartDateC); 
     etFullDesC = (EditText) findViewById(R.id.etFullDescC); 
     etCostC = (EditText) findViewById(R.id.etCostC); 



     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton bSendContest = (FloatingActionButton) findViewById(R.id.bSendContest); 
     bSendContest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Contest contest = new Contest(etContestName.getText().toString(), 
         sCategoryC.getSelectedItem().toString(), 
         etShorDesC.getText().toString(), 
         etDeadlineC.getText().toString(), 
         etAgeLimitC.getText().toString(), 
         etStartDateC.getText().toString(), 
         etLocatoinC.getText().toString(), 
         etFullDesC.getText().toString(), 
         idUserUpload, 
         etCostC.getText().toString()); 

       switch (v.getId()){ 
        case R.id.bSendContest : 
         if(etContestName.getText().toString().matches("") || 
           sCategoryC.getSelectedItem().toString().matches("") || 
           etShorDesC.getText().toString().matches("") || 
           etDeadlineC.getText().toString().matches("") || 
           etAgeLimitC.getText().toString().matches("") || 
           etStartDateC.getText().toString().matches("") || 
           etLocatoinC.getText().toString().matches("") || 
           etFullDesC.getText().toString().matches("") || 
           idUserUpload.matches("") || 
           etCostC.getText().toString().matches("")){ 
          AlertDialog alertDialog = new AlertDialog.Builder(AddContest.this).create(); 
          alertDialog.setTitle("Something went wrong"); 
          alertDialog.setMessage("Please fill all the boxes"); 
          alertDialog.show(); 

         }else { 
          ServerRequest serverRequest = new ServerRequest(AddContest.this); 
          serverRequest.StoreContestInBackground(contest); 
          Snackbar.make(findViewById(R.id.addProjectLayout), "The contest Succesfuly added!", Snackbar.LENGTH_LONG) 
            .setAction("Action", null).show(); 
          startActivity(new Intent(AddContest.this, Dashboard.class)); 
         } 

         break; 
       } 
      } 
     }); 
    } 


    @Override 
    public void onClick(View v) { 


    } 
} 

,但我得到這個錯誤

02-29 20:15:49.950 12260-12260/com.example.torge.projectnow E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.torge.projectnow, PID: 12260 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference 
                        at com.example.torge.projectnow.loginSystem.AddContest$1.onClick(AddContest.java:47) 
                        at android.view.View.performClick(View.java:4788) 
                        at android.view.View$PerformClick.run(View.java:19965) 
                        at android.os.Handler.handleCallback(Handler.java:739) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:135) 
                        at android.app.ActivityThread.main(ActivityThread.java:5431) 
                        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:914) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 

我無法解決它。你可以幫我嗎?

+0

哪一行是錯誤? – Atri

回答

2

您還沒有初始化etLocatoinC

etLocatoinC=(EditText) findViewById(R.id.etLocatoinC); 

,你正試圖調用的getText()方法就可以了。

+0

非常感謝! –

0

你忘了投TextView的etLocatoinConCreate方法,即宣告但空引用... 因爲你是在onclick方法調用gettext的,那麼你得到一個NPE ..

Quickfix:

etLocatoinC=(EditText) findViewById(R.id.etLocatoinC); 
+0

非常感謝! –