如何将Activity作为参数发送给方法?

时间:2015-09-25 11:29:46

标签: android

这里我有一个启动差异活动的方法

public void StartViewActivities(View view, AppCompatActivity activity)
    {
        ViewGroup selectedGroup = (ViewGroup)view;
        NoteObject objectForIntent = new NoteObject();
        objectForIntent.keywords = (String)((TextView)selectedGroup.getChildAt(0)).getText();
        objectForIntent.Details = (String)((TextView)selectedGroup.getChildAt(1)).getText();
        objectForIntent.ID = Integer.parseInt((String)((TextView)selectedGroup.getChildAt(2)).getText());
        startActivity(new Intent(GetActivity.this, activity.class).putExtra("keywords", objectForIntent.keywords)
                .putExtra("Details", objectForIntent.Details).putExtra("ID",objectForIntent.ID));
    }

我将要启动的活动作为参数发送。但在startActivity方法中,它表示未知类:活动。那么我们如何将活动作为参数传递?

3 个答案:

答案 0 :(得分:0)

您应该使用activity.class代替 WifiConfiguration wifiConfig = new WifiConfiguration(); wifiConfig.SSID = String.format("\"%s\"", sr.SSID); wifiConfig.preSharedKey = String.format("\"%s\"", password); WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); int netId=wifiManager.addNetwork(wifiConfig); if (netId != -1) { if (wifiManager.enableNetwork(netId, true)) { sreturn = "CONNECTED"; wifiManager.disableNetwork(netId); } wifiManager.removeNetwork(netId); } return sreturn; }

答案 1 :(得分:0)

您可以使用Class type的变量。如果您愿意,可以将其作为参数传递给方法。

Class clz = MyActivity.class
startActivity(new Intent(context, clz));

答案 2 :(得分:0)

因为你传递了一个很长的Activity实例(AppCompatActivity extends Activity),所以你不能activity.class但你可以activity.getClass()。除此之外,我假设GetActivity是您想要开始新活动的活动?你可以newIntent(this, activity.getClass())

将yor startActivity行替换为: startActivity(new Intent(this, activity.getClass()).putExtra("keywords", objectForIntent.keywords) .putExtra("Details", objectForIntent.Details).putExtra("ID",objectForIntent.ID));