2

当我们从一个activity到另一个activity并从堆栈中删除当前activity时,我得到一个NullPointer exception. 假设我有class Aclass 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

我怎么解决这个问题?

4

4 回答 4

0

我不确定,但是如果您没有从活动 B 传递任何内容并将“b”设为 Null,则可能会发生这种情况。

而不是下面的代码。在您可以通过 Intent 获得所有想要的值之后,您应该首先检查是否b==null 或 Not 。

所以与其

   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");
  }
于 2013-01-21T09:33:53.503 回答
0

尝试使用,getExtras 上面setContentView()

  Intent emailIntent = getIntent();
  Bundle d = emailIntent.getExtras();
  emailReferAgent = d.getString("emailAgent");
于 2013-01-21T09:35:31.497 回答
0

我猜是因为这些的值为空,

   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() 的地方传递额外的东西,因此它们在这里为空,你得到了异常。

如果您不希望您的日志使您的应用程序崩溃,请像这样编辑您的日志,

                Log.e("Name String", name+"");
                Log.e("email String", email+"");
                Log.e("mobile String", mobile+"");

因此,如果值为 null,则打印“null”,如果不是,则打印值本身。

于 2013-01-21T09:31:19.700 回答
0
nameTXT.setText(name);

这似乎是您尝试设置名称文本时的问题,如果名称没有返回任何值,那么它将生成nullpointerexception.

请检查您从 B 类到 A 类时是否获得了价值。

于 2013-01-21T09:44:17.303 回答