当我们从一个activity
到另一个activity
并从堆栈中删除当前activity
时,我得到一个NullPointer exception
. 假设我有class A
和class B
。我要从 A 到 B,然后从 B 回到 A 并使用finish()
方法或使用Flag_clear_Top
.
“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:线程退出未捕获的异常(组= 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
我怎么解决这个问题?