当我们从一个activity
转到另一个activity
并从堆栈中移除当前activity
时,我得到NullPointer exception
。
假设我有class A
和class B
。我将从A转到B,然后从B转到A并使用finish()
方法或使用Flag_clear_Top
从堆栈中删除B活动。
“A”类作为MainClass
public class MainClass extends Activity{
Button callBTN;
Button messageBTN;
Button emailBTN;
Button referBTN;
TextView nameTXT;
String name ;
String email;
String mobile;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_class_layout);
Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
name = b.getString("name");
email = b.getString("email");
mobile = b.getString("mobile");
Log.e("Name String", name);
Log.e("email String", email);
Log.e("mobile String", mobile);
nameTXT=(TextView)findViewById(R.id.name_TXT);
callBTN=(Button)findViewById(R.id.call_BTN);
messageBTN=(Button)findViewById(R.id.message_BTN);
emailBTN=(Button)findViewById(R.id.email_BTN);
referBTN=(Button)findViewById(R.id.refer_BTN);
nameTXT.setText(name);
callBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ mobile));
startActivity(callIntent);
}
});
messageBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("MainClass","to find mobile number"+mobile);
Intent intentSendMessage=new Intent(MainClass.this,SmsSender.class);
intentSendMessage.putExtra("mobile_number",mobile);
startActivity(intentSendMessage);
}
});
emailBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentEmail =new Intent(MainClass.this,EmailSender.class);
intentEmail.putExtra("mail_Str",email);
startActivity(intentEmail);
}
});
referBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentRefer=new Intent(MainClass.this,References.class);
intentRefer.putExtra("emailAgent",email);
startActivity(intentRefer);
}
});
}
}
“B”类作为参考文献:
package com.cql.ins;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewDebug.FlagToString;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.cqlsys.help.UserFunctions;
public class References extends Activity
{
EditText nameEDt,emailEDt,phoneEDt,commentEDT;
TextView errorTXT;
Button referenceSendBTN;
String emailReferAgent;
// JSON Response node names
private static String KEY_SUCCESS = "status";
private static String KEY_Message ="message";
private static String KEY_ERROR = "error";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.references_layout);
Intent emailIntent = getIntent();
Bundle d = emailIntent.getExtras();
emailReferAgent = d.getString("emailAgent");
nameEDt=(EditText)findViewById(R.id.name_Refer_EDt);
emailEDt=(EditText)findViewById(R.id.email_Refer_EDT);
phoneEDt=(EditText)findViewById(R.id.phone_refer_EDT);
commentEDT=(EditText)findViewById(R.id.comment_Refer_EDT);
errorTXT=(TextView)findViewById(R.id.errorTXT);
referenceSendBTN=(Button)findViewById(R.id.refer_send_BTN);
referenceSendBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String nameReferStr = nameEDt.getText().toString();
String emailReferStr = emailEDt.getText().toString();
String phoneReferStr =phoneEDt.getText().toString();
String commentReferStr =commentEDT.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json =userFunction.referUser(nameReferStr, emailReferStr, phoneReferStr,commentReferStr,emailReferAgent);
// check for response
try {
if (json.getString(KEY_SUCCESS) != null) {
errorTXT.setText("hello");
String res = json.getString(KEY_SUCCESS);
Log.e("REferences Status Tag", res);
if(Boolean.parseBoolean(res) == true){
// user successfully submit
//Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
Intent intentbackMainClass=new Intent(References.this,MainClass.class);
//intentbackMainClass.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentbackMainClass);
finish();
}else{
// Error in submit the data
errorTXT.setText("Error occured in registration");
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
但我得到以下例外:
** threadid = 1:线程退出,未捕获异常(group = 0x4001d800) 01-19 02:43:27.507:E / AndroidRuntime(13801):致命异常:主要 01-19 02:43:27.507:E / AndroidRuntime(13801):java.lang.RuntimeException:无法启动活动ComponentInfo {com.cqlsys.insagent / com.cql.ins.MainClass}:java.lang.NullPointerException **
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.os.Looper.loop(Looper.java:123)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 02:43:27.507: E/AndroidRuntime(13801): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 02:43:27.507: E/AndroidRuntime(13801): at java.lang.reflect.Method.invoke(Method.java:521)
01-19 02:43:27.507: E/AndroidRuntime(13801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 02:43:27.507: E/AndroidRuntime(13801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 02:43:27.507: E/AndroidRuntime(13801): at dalvik.system.NativeStart.main(Native Method)
01-19 02:43:27.507: E/AndroidRuntime(13801): Caused by: java.lang.NullPointerException
01-19 02:43:27.507: E/AndroidRuntime(13801): at com.cqlsys.insagent.MainClass.onCreate(MainClass.java:39)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 02:43:27.507: E/AndroidRuntime(13801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 02:43:27.507: E/AndroidRuntime(13801): ... 11 more
我该如何解决这个问题?
答案 0 :(得分:0)
我猜是因为这些值为null,
name = b.getString("name");
email = b.getString("email");
mobile = b.getString("mobile");
但是你试图在这里的日志中打印它们,
Log.e("Name String", name);
Log.e("email String", email);
Log.e("mobile String", mobile);
问题是,我没有看到你从你调用startActivity()的地方传递额外内容,因此它们在这里为null并且你得到了异常。
如果您不希望日志崩溃您的应用,请按照这样编辑您的日志
Log.e("Name String", name+"");
Log.e("email String", email+"");
Log.e("mobile String", mobile+"");
如果值为null,则打印“null”,否则打印值本身。
答案 1 :(得分:0)
我不确定但是如果你没有从活动B传递任何东西并且将“b”作为Null,则会发生这种情况。
而不是代码。在通过Intent获得所需的所有值后,首先应检查 b == null或不。
所以相反
Bundle b = myIntent.getExtras();
name = b.getString("name");
email = b.getString("email");
mobile = b.getString("mobile");
尝试使用以下代码。
Bundle b = myIntent.getExtras();
if(b!=null){
name = b.getString("name");
email = b.getString("email");
mobile = b.getString("mobile");
}
答案 2 :(得分:0)
尝试使用上面setContentView()
Intent emailIntent = getIntent();
Bundle d = emailIntent.getExtras();
emailReferAgent = d.getString("emailAgent");
答案 3 :(得分:0)
nameTXT.setText(name);
这似乎是问题,因为您正在尝试设置名称文本,如果名称没有返回任何值,那么它将生成nullpointerexception
。
请检查从B级到A级是否有价值。