2014-01-11 38 views
-3

我想根據指南製作一個簡單的Android應用程序。我正在使用下面的代碼,但它給了我幾個錯誤。它試圖覆蓋onCreate()方法時抱怨。確切的錯誤在下面。任何人都可以解釋我在這裏犯了什麼錯誤嗎?Android活動中未定義的錯誤

package com.bignerdranch.android.geoquiz; 

import android.os.Bundle; 

public class CheatActivity { 

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

} 

確切的錯誤:

The method onCreate(Bundle) is undefined for the type Object 
The method setContentView(int) is undefined for the type CheatActivity 
The method onCreate(Bundle) of type CheatActivity must override or implement 
a supertype method 
+2

由於錯誤指示,代碼是*不正確。 – user2864740

+1

代碼不正確。你需要''擴展''活動'。 – Emmanuel

+3

停止downvoting,他只是要求幫助,並犯了一個錯誤。它不值得-6或更多。 – Paul

回答

7
import android.app.Activity; 

public class CheatActivity extends Activity { 

你應該從活動類擴展。因爲你只是在沒有任何方法可以從父類重寫的情況下創建新類。

+0

謝謝。我犯了一個愚蠢的錯誤。 –

+0

沒問題。祝你今後的發展順利! – Anatol

0

正如Anatol所說,您必須添加extends Activity

如果您不知道這一點,則只有從Activity中擴展才能實現您的唯一方法。

我會建議您使用IDE的嚮導創建活動。而且您將避免必須手動將它們添加到清單並添加未實現的方法。