2011-03-29 83 views
-2

我得到一個錯誤:該方法OnClickListener()是未定義的類型查看安卓:該方法OnClickListener()是未定義的類型查看

我是新來的Android開發人員。這是我的代碼:

package com.example.playword; 

import java.io.IOException; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class PlayWord extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //final Handler mHandler = new Handler(); 

     final TextView nicholas = (TextView) findViewById(R.id.nicholas); 
     final TextView was = (TextView) findViewById(R.id.was); 

     nicholas.setText("Nicholas "); 
     was.setText("was "); 

     View.OnClickListener handler = View.OnClickListener(){ 
      public void onClick(View v) { 
       switch (v.getId()) { 
        case R.id.nicholas: // doStuff 
         nicholas.setText("Nicholas (Clicked!) "); 
         break; 
        case R.id.was: // doStuff 
         was.setText("was (Clicked!) "); 
         break; 
       } 
      } 
     } 

     findViewById(R.id.nicholasBtn).setOnClickListener(handler); 
     findViewById(R.id.wasBtn).setOnClickListener(handler); 

    } 
} 

在這些線路的錯誤亮點:

 View.OnClickListener handler = View.OnClickListener(){ 
      public void onClick(View v) { 

提前非常感謝! :)

回答

2

你忘了new關鍵字和分號:

View.OnClickListener handler = new View.OnClickListener(){ 
    ... 
}; 
相關問題