gpt4 book ai didi

java - EditText.GetText,返回有关空引用的异常,但已声明

转载 作者:行者123 更新时间:2023-12-02 03:53:10 27 4
gpt4 key购买 nike

我正在开发 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)

我无法解决这个问题。你能帮我吗?

最佳答案

您尚未初始化 etLocatoinC

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

并且您正在尝试对其调用 getText() 方法。

关于java - EditText.GetText,返回有关空引用的异常,但已声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35707010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com