我正在开发Android应用程序,但我遇到了一些问题。
我有一些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 :(得分:2)
您尚未初始化etLocatoinC
即
etLocatoinC=(EditText) findViewById(R.id.etLocatoinC);
并且您正尝试在其上调用getText()方法。
答案 1 :(得分:0)
您忘记在etLocatoinC
方法中强制转换TextView onCreate
,该方法已声明但引用为null ...
因为你在onClick方法中调用了getText,所以你得到了一个NPE ..
etLocatoinC=(EditText) findViewById(R.id.etLocatoinC);