簡體   English   中英

如何通過意圖setclass從父活動中調用其他活動?

[英]How to call an other activity from parent activity by intent setclass?

我正在嘗試通過已定義的字符串值的處理使用baseActivity來進行動態移動活動處理。
但是我遇到了“活動不是封閉類”的問題。 調用baseActivity的“ newIntent.setClassName”方法時出錯。
你能幫個忙嗎?

首先,為了解釋嘗試代碼,

此過程用於用戶注冊過程。

這是二手活動。
1. AgreeChildActivity(同意服務條款)
2.VerificationingChildActivity(驗證用戶)
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.");
   }

嘗試-

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

您必須使用現有的活動上下文來啟動新活動,尚未創建新活動,並且不能使用其上下文或對其調用方法。

不會因為您使用此關鍵字而引發封閉類錯誤 這是對當前對象的引用—當前對象的方法或構造函數正在被調用。 使用此方法,您只能從實例方法或構造函數中引用當前對象的任何成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM