2011-12-05 34 views
-1
package com.duncan.hello.world; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

import com.duncan.hello.world.R; 

public class HelloWorldActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    Button aButton; 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    aButton = (Button) this.findViewById(R.id.button1); 

    aButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(HelloWorld.this, OtherActivity.class); 
      startActivity(i); 
     }}); 
} 
} 

我現在只有一個錯誤,因爲我導入了按鈕。錯誤位於第23行,並顯示「HelloWorld無法解析爲類型」我能對這些錯誤做些什麼?

+3

你可以導入你使用的類,對於初學者:) –

+0

-1爲沒有建設性的問題。 – WilHall

+0

這是來自我最後一個問題的教程,所以不要吼我。 –

回答

1

您需要導入正在使用的類。由於您尚未導入按鈕,因此無法將按鈕解析爲類型。

它應該給你一個錯誤之前,你甚至可以運行與這樣的錯誤的代碼。

+0

我甚至沒有嘗試運行代碼,因爲我無法運行錯誤的代碼,無論如何,你說要添加像導入android.view.button? –

+0

是的。你在使用eclipse嗎? –

+0

您是否已經將Android SDK添加到IDE?如果你使用eclipse,Android有一個插件在這裏.. http://developer.android.com/sdk/installing.html –

0

您的活動名稱是HelloWorldActivity,但您在意圖中使用了HelloWorld。 替換此代碼,

Intent i = new Intent(HelloWorld.this, OtherActivity.class); 

通過下面的代碼替換上面,

Intent i = new Intent(HelloWorldActivity.this, OtherActivity.class); 
相關問題