Java SubscribeActivity中的Edittext异常:空指针异常错误
我有订阅活动的代码,但当我输入send按钮插入时,我得到了致命的异常,我在google中检查了它,但我没有找到解决问题的方法。连接成功消息后,我收到错误消息。我已经添加了互联网许可,那么可能会有什么问题,任何人都可以帮助吗Java SubscribeActivity中的Edittext异常:空指针异常错误,java,android,android-edittext,fatal-error,Java,Android,Android Edittext,Fatal Error,我有订阅活动的代码,但当我输入send按钮插入时,我得到了致命的异常,我在google中检查了它,但我没有找到解决问题的方法。连接成功消息后,我收到错误消息。我已经添加了互联网许可,那么可能会有什么问题,任何人都可以帮助吗 public class SubscribeActivity extends Activity { String result = null; InputStream is = null; private EditText edittext_firs
public class SubscribeActivity extends Activity {
String result = null;
InputStream is = null;
private EditText edittext_first;
private EditText edittext_last;
private EditText edittext_age;
private EditText edittext_email;
private EditText edittext_confirm;
private Button send;
String v5,v1,v2,v3,v4,v6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe);
edittext_first = (EditText) findViewById(R.id.firstname);
edittext_last = (EditText) findViewById(R.id.lastname);
edittext_age = (EditText) findViewById(R.id.age1);
edittext_email = (EditText) findViewById(R.id.email);
edittext_confirm = (EditText) findViewById(R.id.confirm);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View view) {
boolean isValid = true;
if (edittext_first.getText().toString().length() <= 0) {
edittext_first.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_first.getText().toString().matches("[a-zA-Z ]+")) {
edittext_first.setError("ENTER ONLY ALPHABETS");
isValid = false;
}
if (edittext_last.getText().toString().length() <= 0) {
edittext_last.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_last.getText().toString().matches("[a-zA-Z ]+")) {
edittext_last.setError("ENTER ONLY ALPHABETS");
isValid = false;
}
if (edittext_age.getText().toString().length() <= 0) {
edittext_age.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_age.getText().toString().matches("[0-9 ]+")) {
edittext_age.setError("ACCEPT NUMBERS");
isValid = false;
}
if (edittext_email.getText().toString().length() <= 0) {
edittext_email.setError("VALUE REQUIRED");
isValid = false;
} else if (!isEmailValid(edittext_email.getText().toString())) {
edittext_email.setError("INVALID EMAIL");
isValid = false;
}
if (edittext_confirm.getText().toString().length() <= 0) {
edittext_confirm.setError("VALUE REQUIRED");
isValid = false;
} else if (!isEmailValid(edittext_confirm.getText().toString())) {
edittext_email.setError("INVALID EMAIL");
isValid = false;
} else if (!edittext_confirm.getText().toString().equals(edittext_email.getText().toString())) {
edittext_confirm.setError("PASSWORD MISMATCH");
isValid = false;
}
v1 = edittext_first.getText().toString();
v2 = edittext_last.getText().toString();
v3 = edittext_age.getText().toString();
v4 = edittext_email.getText().toString();
v5 = edittext_confirm.getText().toString();
if (isValid) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("firstname", v1));
nameValuePairs.add(new BasicNameValuePair("lastname", v2));
nameValuePairs.add(new BasicNameValuePair("age", v3));
nameValuePairs.add(new BasicNameValuePair("email_address", v4));
nameValuePairs.add(new BasicNameValuePair("confirm_email_address", v5));
submitDataOnServer(nameValuePairs);
}
}
});
}
public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137/Android/App/getInsert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
System.out.println("Zero");
//Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try {
System.out.println("First");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
System.out.println("Second");
System.out.println("After Second:"+reader.readLine());
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
System.out.println("Third");
result=sb.toString();
} catch (Exception e) {
System.out.println("Fourth");
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
System.out.println("Fifth");
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("result");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
System.out.println("sixth");
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}
public boolean isEmailValid(final String mailAddress) {
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(mailAddress);
return matcher.matches();
}
}
公共类SubscribeActivity扩展活动{
字符串结果=null;
InputStream=null;
私有EditText EditText_优先;
私有EditText EditText_last;
私人编辑文本编辑文本;
私人编辑文本编辑文本电子邮件;
私人编辑文本编辑文本确认;
私人按钮发送;
字符串v5、v1、v2、v3、v4、v6;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe);
edittext_first=(edittext)findViewById(R.id.firstname);
edittext_last=(edittext)findViewById(R.id.lastname);
edittext_age=(edittext)findViewById(R.id.age1);
edittext_email=(edittext)findviewbyd(R.id.email);
edittext_confirm=(edittext)findViewById(R.id.confirm);
send=(按钮)findviewbyd(R.id.send);
send.setOnClickListener(新视图.OnClickListener(){
@SuppressLint(“新API”)
@凌驾
公共void onClick(视图){
布尔值isValid=true;
如果(edittext_first.getText().toString().length()在doInBackground中,则返回null
请
从doInBackground
public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137/Android/App/getInsert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
System.out.println("Zero");
//Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try {
System.out.println("First");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
System.out.println("Second");
System.out.println("After Second:"+reader.readLine());
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
System.out.println("Third");
result=sb.toString();
} catch (Exception e) {
System.out.println("Fourth");
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
System.out.println("Fifth");
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("result");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
System.out.println("sixth");
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}
public void submitdata服务器(最终ArrayList nameValuePairs){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://192.168.1.137/Android/App/getInsert.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“Log_标签”,“连接成功”);
System.out.println(“零”);
//Toast.makeText(getApplicationContext(),“订阅成功。感谢您订阅,您现在已被添加到我们的邮件列表”,Toast.LENGTH_LONG.show();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
//Toast.makeText(getApplicationContext(),“连接失败”,Toast.LENGTH\u SHORT.show();
}
试一试{
System.out.println(“第一”);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
System.out.println(“第二”);
System.out.println(“第二个之后:+reader.readLine());
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
系统输出打印项次(“第三次”);
结果=sb.toString();
}捕获(例外e){
系统输出打印项次(“第四次”);
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
试一试{
系统输出打印号(“第五”);
JSONObject json_data=新的JSONObject(结果);
CharSequence w=(CharSequence)json_data.get(“结果”);
Toast.makeText(getApplicationContext(),w,Toast.LENGTH_SHORT).show();
}捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
系统输出打印号(“第六”);
Toast.makeText(getApplicationContext(),“JsonArray失败”,Toast.LENGTH\u SHORT.show();
}
}
}.execute();
}
另一个.java的第31行是什么?那一行出现空指针异常。你能显示那一行吗?@annchoor那一行就是问题所在。我们无法在不知道代码的情况下帮助你。一步一步解决错误。首先,解决这个NPE。然后解决另一个错误。第JSONTokener.java:116行有NPE。所以请不要删除导致问题的g行。相反,解决它们。检查导致空指针的原因并防止它!愉快的编码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="@drawable/deadoralive" />
<requestFocus
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/record_sofya"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Join Our Mailing List - Join Today to Receive your Free Download"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Name"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/lastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Last Name"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/age1"
android:numeric="integer"
android:maxLength="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email Address"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Email Address"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:text="Send"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137/Android/App/getInsert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
System.out.println("Zero");
//Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try {
System.out.println("First");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
System.out.println("Second");
System.out.println("After Second:"+reader.readLine());
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
System.out.println("Third");
result=sb.toString();
} catch (Exception e) {
System.out.println("Fourth");
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
System.out.println("Fifth");
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("result");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
System.out.println("sixth");
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}