Android 当我从一个活动转到另一个活动时,我得到了空指针异常

Android 当我从一个活动转到另一个活动时,我得到了空指针异常,android,android-activity,nullpointerexception,flags,Android,Android Activity,Nullpointerexception,Flags,当我们从一个活动转到另一个活动并从堆栈中删除当前的活动时,我得到一个空指针异常。 假设我有A类和B类。我从A转到B,然后从B回到A,使用finish()方法或使用Flag\u clear\u Top从堆栈中删除B活动 “A”类作为主类 public class MainClass extends Activity{ Button callBTN; Button messageBTN; Button emailBTN; Button referBTN; T

当我们从一个
活动
转到另一个
活动
并从堆栈中删除当前的
活动
时,我得到一个
空指针异常
。 假设我有
A类
B类
。我从A转到B,然后从B回到A,使用
finish()
方法或使用
Flag\u clear\u Top
从堆栈中删除B活动

“A”类作为主类

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:无法启动活动组件信息{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

我该如何解决这个问题呢?

我想这是因为它们的值是空的

   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”,如果不是,则打印值本身

我不确定,但如果您没有从活动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");
  }

尝试使用上面的getExtras
setContentView()

这似乎是您试图设置名称文本时遇到的问题,如果名称未返回任何值,则它将生成
nullpointerexception


请检查从B类到A类时是否得到值。

在哪一行u得到此错误?如果您没有传递bundle,并使用intent.putExtra(“Str”,Str)传递,则可以使用intent.getStringExtra(“Str”)检索它;您是否在日志猫中打印了bundle的所有值?@Krishna:当我在类引用中使用intent调用堆栈中已经存在的上一个活动时,我得到了错误:我的代码为//用户成功提交Toast.makeText(getApplicationContext(),“msg msg”,Toast.LENGTH_SHORT).show();//Intent intentbackMainClass=new Intent(References.this,MainClass.class);//intentbackMainClass.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);//startActivity(intentbackMainClass);我在01-19 04:36:47.018:E/REferences状态标记(14045):true 01-19 04:36:47.258:E/AndroidRuntime(14045):致命异常:main 01-19 04:36:47.258:E/AndroidRuntime(14045):java.lang.RuntimeException:无法启动活动组件信息{com.cqlsys.insagent/com.cqlsys.insagent.MainClass}:java.lang.NullPointerExceptionHi Bhavesh-感谢您的回复,我已经这样做了,但错误仍然存在。我解释了错误是如何发生的。我从“MainClass”活动转到“References”活动,然后再从“References”活动回到MainClass活动。我的“引用”成功获得JSON响应。-请尝试{if(JSON.getString(KEY_SUCCESS)!=null){errorTXT.setText(“hello”);String res=JSON.getString(KEY_SUCCESS);if(Boolean.parseBoolean(res)==true){Intent intentbackMainClass=newIntent(References.this,MainClass.class);intentbackMainClass.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);startActivity(intentbackMainClass);}
  Intent emailIntent = getIntent();
  Bundle d = emailIntent.getExtras();
  emailReferAgent = d.getString("emailAgent");
nameTXT.setText(name);