Android:动态开始活动 [英] Android: Dynamically starting an activity

查看:69
本文介绍了Android:动态开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据前一个活动的输入动态地启动一个活动.我已经通过上一个活动输入了一个字符串,唯一的是此特定代码引发了错误

I would like to dynamically start an activity based on the previous activity's input. I have input a string through the previous activity, the only thing is this specific code throws the error

无法解析构造函数'Intent(com.MentalMathWorkout.EasyCountDown,java.lang.String)'

cannot resolve constructor 'Intent(com.MentalMathWorkout.EasyCountDown, java.lang.String)'

有没有办法使这项工作成功?

Is there a way to make this work?

public class EasyCountDown extends AppCompatActivity {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ecd);

    Intent intent = getIntent();
    String test = intent.getStringExtra(MainActivity.TEST_TYPE);
    String cstring = ".class";
    final String activity = test.concat(cstring);

    Intent intent = new Intent(EasyCountDown.this, activity);
    startActivity(intent); //Start test
}

推荐答案

ComponentName对象就是这样:

The ComponentName object does just that:

String activity = intent.getStringExtra(MainActivity.TEST_TYPE);
Intent intent = new Intent(this, new ComponentName(this, activity));
startActivity(intent);

假设 this Activity 的一个实例.(对于 Fragment ,请使用 getActivity(),参见)

That's assuming this is an instance of Activity. (for a Fragment, use getActivity(), obv.)

这篇关于Android:动态开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆