2012-04-07 79 views
0

我想從一個活動發送值到另一個,但我得到空指針 異常請解決我的問題。第一個活動包含短信的詳細信息
短信發送到第二基於這些值的活動第二活動搜索聯繫人 否併發送回復短信。從一個活動傳遞值到另一個

public void onReceive(Context context, Intent intent) 
    { 
     // Get SMS map from Intent 
     Bundle bundle = null; 
     Bundle extras = intent.getExtras(); 

     String messages = ""; 
     String address = null,body=null; 

     if (extras != null) 
     { 
      // Get received SMS array 
      Object[] smsExtra = (Object[]) extras.get("pdus"); 

      // Get ContentResolver object for pushing encrypted SMS to incoming folder 
      //ContentResolver contentResolver = context.getContentResolver(); 

      for (int i = 0; i < smsExtra.length; ++i) 
      { 
       SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 

       body = sms.getMessageBody().toString(); 
       address = sms.getOriginatingAddress(); 

       messages += "SMS from " + address + " :\n";      
       messages += body + "\n"; 

       // Here you can add any your code to work with incoming SMS 
       // I added encrypting of all received SMS 


      } 

      // Display SMS message 
      Toast.makeText(context, messages, Toast.LENGTH_SHORT).show(); 
      Intent i=new Intent(context,AlertActivity.class); 

      bundle.putString("from",address); 
      bundle.putString("msg",body); 
      i.putExtras(bundle); 

      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(i); 

     } 

} 

活性2:

Intent i=getIntent(); 
    Bundle bundle=i.getExtras(); 
    String fromAdd=bundle.getString("from"); 
    String msgBody=bundle.getString("body"); 
+0

[**請參見本博客。這可以幫助你**](http://startandroiddevelopment.blogspot.in/2013/11/how-to-pass-boolean-int-string-integer.html) – 2013-11-04 04:51:17

回答

1

更改此

String msgBody=bundle.getString("body"); 

String msgBody=bundle.getString("msg"); 
1

試試這個:

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main);  
Bundle bundle = this.getIntent().getExtras(); 
if (bundle != null) 
{  
String fromAdd = bundle.getString("from"); 
String msgBody = bundle.getString("body"); 
}   
} 
1

在Android Bundle中,我們把關鍵值對,並且在從數據包中獲取數據的同時通過相同的密鑰是強制性的。檢查你的代碼你把兩個字符串的意圖,它們是: 「從」和「味精」 和你是關鍵正從意圖值:「從」 和「身體」

所以更改它要麼在開始活動或活動2中,以便鍵值匹配。

0

試試這個.....

Activity1.java

Intent intent = new Intent(getApplication(),Activity2.class); 
intent.putExtra("from",from); 
intent.putExtra("Body",Body); 
StartActivity(intent); 

Activity2.java

Intent intent = getintent(); 
Bundle bundle=intent.getExtras(); 
String Body=bundle.getString("Body"); 
String From=bundle.getString("From"); 
setResult("RESULT_OK",intent); 
0

嘗試用這種方式..

捆綁BU = getIntent( ).getExtras();

String title = bu.get(「from」)。toString();

String msg = bu.get(「body」)。toString();

0

試試這個......將值從Activity1傳遞給Activity2。

Intent myIntent = new Intent(Activity1.this, Activity2.class); 
      myIntent.putExtra("UserId",UserId); 
      myIntent.putExtra("UserName",UserName); 
      myIntent.putExtra("CompanyID",CompanyID); 
      myIntent.putExtra("CompanyName",CompanyName); 
      myIntent.putExtra("ProjectId",ProjectId); 
      startActivity(myIntent); 

還可以提取值可以使用

Intent intent = getIntent(); 
    UserId=intent.getStringExtra("UserId"); 
    UserName=intent.getStringExtra("UserName"); 
    CompanyID=intent.getStringExtra("CompanyID"); 
    CompanyName=intent.getStringExtra("CompanyName"); 
相關問題