如何通过intent setclass从父活动调用其他活动?

时间:2016-02-15 05:29:58

标签: android android-intent

我试图通过定义字符串值的过程使用baseActivity来创建动态移动活动过程。
但是我遇到了一个问题,即活动不是封闭的课程。"调用错误" newIntent.setClassName" baseActivity的方法。
你能帮一些想法吗?

首先,为了解释尝试代码,

此流程用于用户注册过程。

这是用过的活动。
1. AgreeChildActivity(同意服务条款)
2. VerifyingChildActivity(验证用户)
3. InputChildActivity(输入用户信息)
4. CompleteChildActivity(显示服务加入完成)

1~4这些是" GateBaseActivity"。

的孩子


它通过" MainActivity"的onClick方法启动动态移动过程。

MainActivity {
    String sProcessCase1 = "verifying->input->complete";
    String sProcessCase2 = "input->verifying->complete";
    String sProcessCase3 = "input->complete";

    :
    :

    public void onClick(View v) {
        Intent intent = null;
        try {
            switch (v.getId()) {
                case R.id.goDynamicMenu:
                   // start dynamic process
                   Intent newIntent = new Intent()
                   newIntent.setClassName(this, "AgreeChildActivity");
                   newIntent.putExtra("MOVE_SEQ", sProcessCase1);   <== set processCase.
                   startActivity(newIntent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


// Following is each activities code.

GateBaseActivity (<= This is a parent of all process activity.) 
   void onResume() { (so executes this method when each process activity shows.)
       String sActivitySeq = getintent("MOVE_SEQ"); <= "verifying->input->complete"
       String sNextActivity = <= calculate the nextActivity name on sActivitySeq.  For simple, omitted. 
       // "verifying->input->complete"
       // verifying => VerifyingChildActivity
       // input => InputChildActivity
       // complete => CompleteChildActivity

       String sThisChildSimpleName = this.getClass().getSimpleName();

       Intent newIntent = new Intent();

       // execute here when childActivity is the "AgreeChildActivity".
       if ("AgreeChildActivity".equals(sThisChildSimpleName)) {
           newIntent.setClassName(AgreeChildActivity(<=Here occured "not an enclosing class err.", How to solve this error?).this, sNextActivity);
       }

       // execute here when childActivity is the "VerifyingChildAcitivty".
       else if ("VerifyingChildActivity".equals(sThisChildSimpleName)) {
           newIntent.setClassName(VerifyingChildActivity(<=Here occured "not an enclosing class err.", How to solve this error?).this, sNextActivity);
       }

       // execute here when childActivity is the "InputChildAcitivty".
       else if ("InputChildActivity".equals(sThisChildSimpleName)) {
           newIntent.setClassName(InputChildActivity(<=Here occured "not an enclosing class err.", How to solve this error?).this, sNextActivity);
       }

       // execute here when childActivity is the "CompleteChildActivity".
       else if ("CompleteChildActivity".equals(sThisChildSimpleName)) {
           newIntent.setClassName(CompleteChildActivity(<=Here occured "not an enclosing class err.", How to solve this error?).this, sNextActivity);
       }

       newIntent.putExtra("MOVE_SEQ", sActivitySeq);
       startActivity(intent); 
   }


// These are child activities of "GateBaseActivity".

AgreeChildActivity implement GateBaseActivity
   void onResume() {
       super.onResume();
       Log.d("autoTest", "Here is AgreeChildActivity.");
   }

VerifyingChildActivity implement GateBaseActivity
   void onResume() {
       super.onResume();
       Log.d("autoTest", "Here is VerifyingChildActivity.");
   }

InputChildActivity implement GateBaseActivity
   void onResume() {
       super.onResume();
       Log.d("autoTest", "Here is InputChildActivity.");
   }

CompleteChildActivity implement GateBaseActivity
   void onResume() {
       super.onResume();
       Log.d("autoTest", "Here is CompleteChildActivity.");
   }

1 个答案:

答案 0 :(得分:1)

尝试 -

Context context = getApplicationContext();
Intent intent = new Intent(context, AgreeChildActivity.class);

您必须使用现有活动上下文来启动新活动,尚未创建新活动,并且您无法使用其上下文或调用方法。

由于您使用了此关键字,因此抛出了

没有封闭类错误。这是对当前对象的引用 - 正在调用其方法或构造函数的对象。使用此方法,您只能从实例方法或构造函数中引用当前对象的任何成员。