簡體   English   中英

Android:你如何點擊其他活動?

[英]Android: How do you go to another activity on click?

我正試圖按順序進入第三個活動。 從主要活動到第二個活動工作正常,但是當我嘗試從第二個活動轉到第三個活動時,應用程序崩潰了。

這是我的第二個活動的代碼:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class Game extends Activity implements OnClickListener {

    private static final String TAG = "Matrix";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.matrix);
        View doneButton = findViewById(R.id.done_button);
        doneButton.setOnClickListener(this);
    }

    public void onClick(View v) { 
        switch (v.getId()) { 
            case R.id.done_button:
                Intent k = new Intent(this, GameTwo.class);
                startActivity(k);
                //finish();
                break;
        }
    }
}

以及第三項活動的代碼:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class GameTwo extends Activity {

   private static final String TAG = "Matrix";

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.matrixtwo);
       View donetwoButton = findViewById(R.id.donetwo_button);
   }
}

switch嘗試以下代碼:

try {
    Intent k = new Intent(Game.this, GameTwo.class);
    startActivity(k);
} catch(Exception e) {
    e.printStackTrace();
}

告訴我這有用.....

Intent k = new Intent(Game.this, GameTwo.class);
startActivity(k);

這有效,但您還要確保在清單中指定此項。

確保在清單中聲明了三個活動。 創建活動是一個常見錯誤,而不是聲明它。

使用以下方法調用新活動:

Intent k = new Intent(Game.this, GameTwo.class);
startActivity(k);

試試這個

Intent intent = new Intent(getApplicationContext(), GameTwo.class);
startActivity(intent);

這是一個很長的鏡頭,但......
你的問題也可能是由NullPointerException引起的
如果在donetwo_button中未聲明matrixtwo.xml ,則拋出matrixtwo.xml ...
(復制粘貼錯誤很常見)

暫無
暫無

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

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