2011-01-25 98 views
7

我有两个活动,如Activity甲和乙,我试图通过使用BundlestartActivity(intent)从A到B两个不同的字符串。如何从android中的其他活动获取数据?

就像是:

Intent intent = new Intent(A.this, B.class); 
Bundle bundle = new Bundle(); 
bundle.putString("vidoedetails", filedetails); 
//bundle.putString("videoname", filename); 


intent.putExtras(bundle); 
//intent.putExtra("videofilename", filename); 
//intent.putExtra("vidoefiledetails", filedetails); 
startActivity(intent); 

而在B类,我用两个TextView s到seperately显示从A类的字符串。

就像是:

Intent i = getIntent(); 
Bundle extras = i.getExtras(); 

filedetails = extras.getString("videodetails"); 
filename = extras.getString("videoname"); 

问题是filedetils得到印在B类而不是文件名。

任何解决方案?

回答

19

你有一个错字:

bundle.putString("vidoedetails", filedetails); 

应该

bundle.putString("videodetails", filedetails); 
+33

花了我40秒来发现差异:S – Tanin 2011-11-18 08:49:44

2

是的,你拼写错误videodetails:
此致:VID * OE *详情
正确:VID * EO *详情

9

我知道我对这个答案迟了9天,但这是为什么我创建一个常量类的一个很好的例子。使用常量类时,如果拼写错误(“视频” - >“vidoe”),则无关紧要,因为在您通过众所周知的位置引用它时,这两个位置都会拼写错误。

Constants.java

public static String WELL_KNOWN_STRING "org.example.stackoverflow.4792829"; 

Activity1.java

bundle.putString(Constants.WELL_KNOWN_STRING, filedetails); 

Activity2.java

filedetails = extras.getString(Constants.WELL_KNOWN_STRING);   
+0

总是这样做。如果你不这样做,你会因为字符串不匹配错误而浪费大量时间。当你浪费时间与字符串不匹配的错误,太阳会下降,它会变暗,没有你注意到。天黑时,如果没有注意到,就会被gr e吃掉。不要被grue吃掉。将您的关键字串定义为常量。 – 2015-02-17 23:49:38

0
// First activity 
actvty_btn.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {    
     Intent i = new Intent(v.getContext(),SECONDACTIVITY.class);  
     startActivityForResult(i, STATIC_INTEGER_VALUE); 
    } 
}); 

/* This function gets the value from the other activity where we have passed a value on calling this activity */ 
public void activity_value() { 
    Intent i = getIntent(); 
    Bundle extras=i.getExtras(); 
    if(extras !=null) { 
     // This is necessary for the retrv_value 
     rtrv_value = extras.getString("key"); 

     if(!(rtrv_value.isEmpty())) { 
      // It displays if the retrieved value is not equal to zero 
      myselection.setText("Your partner says = " + rtrv_value); 
     } 
    } 
} 


// Second activity 
myBtn.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("key", txt1.getText().toString()); 
     // Here key is just the "Reference Name" and txt1 is the EditText value 
     intent.putExtras(bundle);    
     startActivity(intent); 
    } 
}); 
0

这里的另一种方式传递活动之间的数据。这只是我遵循的教程中的一个例子。我有一个5秒运行闪屏,然后它会杀死声音片段来自:

@Override 
protected void onPause() { 
    super.onPause(); 
    ourSong.release(); 
} 

我决定我想要的声音剪辑继续播放到下一个活动,同时仍然能够杀死/释放从那里开始,所以我制作了声音剪辑,MediaPlayer对象,public和static,类似于System.out是一个公共静态对象。作为Android开发的新手,对Java开发并不陌生,我是这样做的。

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 

public class Splash extends Activity { 

    public static MediaPlayer ourSong; // <----- Created the object to be shared 
             // this way 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep); 
     ourSong.start(); 
     Thread timer = new Thread() { 
      public void run() { 
       try { 
        sleep(5000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        Intent openStartingPoint = new Intent(
          "expectusafterlun.ch.androidtutorial.MENU"); 
        startActivity(openStartingPoint); 
       } 
      } 
     }; 
     timer.start(); 
    } 
} 

然后从下一个活动,或任何其他活动,我可以访问MediaPlayer对象。

public class Menu extends ListActivity { 

    String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4", 
      "example5", "example6" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(Menu.this, 
       android.R.layout.simple_expandable_list_item_1, activities)); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Splash.ourSong.release(); // <----- Accessing data from another Activity 
            // here 
    } 

} 
相关问题