我有两个活动,第一个活动有一个名为songurl的公共String var,按钮导致了第二个活动,第二个活动有mediaplayer从第一个活动中的String var接收URL,然后开始播放,但问题是mediaplayer没有不起作用,我遇到了错误
这里是MainActivity1.kt或.java作为您的语言:-
class Magazine {
constructor() {
this.hamburgers = [];
}
sumHamburgers() {
return this.hamburgers.map(el => el.price).reduce((p, c) => p + c);
}
hamburgersAvgN() {
return this.sumHamburgers() / Hamburger.counter;
}
toStorage() {
let serialCounter = JSON.stringify(Hamburger.counter);
let serialAvgN = JSON.stringify(this.hamburgersAvgN());
return localStorage.setItem(['orders', 'avg'], [`number of orders: ${serialCounter}`, ` average check: ${serialAvgN} UAH`]);
}
}
class Hamburger {
constructor(size, ...stuffing) {
this.size = size;
this.stuffing = stuffing;
this.toppingArr = [];
this.counter = ++Hamburger.counter;
}
calculatePrice() {
const stuffingCost = this.stuffing.map(el => el.price).reduce((p, c) => p + c);
let allCost;
if (this.toppingArr.length === 0) {
return allCost = this.size.price + stuffingCost;
} else {
const toppingCost = this.toppingArr.map(el => el.price).reduce((p, c) => p + c);
return allCost = this.size.price + stuffingCost + toppingCost;
}
}
}
Hamburger.counter = 0;
Hamburger.small = {
name: 'small',
price: 10,
kcal: 200
}
Hamburger.cheese = {
name: 'cheese',
price: 4,
kcal: 10
}
Hamburger.meet = {
name: ' meet',
price: 40,
kcal: 103
}
let magazine = new Magazine;
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.cheese));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));
console.log(magazine.sumHamburgers());
这是 MainActivity2:-
lateinit var songurl :String
class MainActivity1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
songurl = "https//...... My URL"
var btnstart_MainActivity2:Button = findViewById(R.id.btn)
btnstart_MainActivity2.setOnClickListener {
startActivity(Intent(this,Main2Activity::class.java))
}
}
}
这是错误
class Main2Activity : AppCompatActivity() {
lateinit var mediaplayer: MediaPlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mediaplayer= MediaPlayer()
mediaplayer.setDataSource(songurl)
mediaplayer.prepare()
mediaplayer.start()
}
}
答案 0 :(得分:0)
您的问题是您的songurl
仅在您的第一次活动中出现。活动2中没有任何价值甚至分配。
考虑将其作为意图的bundle参数传递:
Activity1:
Bundle bundle= new Bundle();
bundle.putString("key", songurl);
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtras(bundle);
startActivity(i);
Activity2:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_activity);
Bundle p = getIntent().getExtras();
String songurl = p.getString("key");
}
示例在Java中。
答案 1 :(得分:0)
您应该在Android Intent Extras中发送songUrl
。出现错误的原因是,它未在您在活动1中定义的活动2中定义。
发送更多意向
var intent = Intent(this,Main2Activity::class.java)
intent.putExtra("songurl", songurl)
获得更多其他活动
if(getIntent().hasExtra("songurl"){
var songurl = getIntent().getStringExtra("songurl")
}