2016-08-04 168 views
-1

我遇到了一些與我的android按鈕有關的問題。當我按下它,我的應用程序崩潰,並且在我的logcat,我有以下錯誤:Android按鈕無法啓動活動

Caused by android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.defendthecastle/com.example.defendthecastle.GameView}; have you declared this activity in your AndroidManifest.xml? 

奇怪的是,那我開始當我點擊我的按鈕,該活動並非GameView,它的GameActivity(使用GameView作爲其視圖)。我之前沒有在清單中聲明的​​活動,所以我這樣做了,但我仍然得到相同的錯誤。 這是我建立了我的按鈕:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    View startButton = findViewById(R.id.start_button); 
    startButton.setOnClickListener(this); 
} 

public void onClick(View v) { 
    switch(v.getId()) { 
    case R.id.start_button: 
     Intent i = new Intent(this, GameActivity.class); 
     startActivity(i); 
     break; 
    } 
} 

這裏是我的XML按鈕:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/start" 
    android:id="@+id/start_button" 

    /> 

最後,這裏是我的GameActivity類:

package com.example.defendthecastle; 


import android.app.Activity; 
import android.os.Bundle; 

public class GameActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    GameView gameView = new GameView(this); 
    setContentView(gameView); 
    gameView.requestFocus(); 
} 

} 

我知道這不是我的GameView的問題,因爲我嘗試使用另一個我認識的應用程序中的另一個視圖。這真是令人沮喪,因爲這只是我的按鈕的問題。任何幫助將不勝感激!

+0

發佈您的清單 –

+1

顯示您的清單 –

+0

您在這裏的第一堂課是什麼? 'MainActivity'?顯示Manifest以及GameView類。目前還不清楚爲什麼系統認爲你試圖開始這種觀點作爲活動 –

回答

-1

聲明您在Manifest文件中的活動,否則它將不會被調用。

注意:確定它應該在Manifest文件的應用程序標記中。

<activity android:name="com.example.defendthecastle.GameView"/> 
+1

再次閱讀錯誤。那是錯誤的課程。 –

+0

感謝您的編輯,但是帖子明確指出,類是一個視圖,而不是活動類 –

+0

,如果它的視圖不是一個活動,那麼爲什麼android.content.ActivityNotFoundException顯示與'GameView'名稱 –