2016-09-02 92 views
-4

當我運行這個程序時,它反覆說「無法解析符號setOnClickListener」。所以請幫我解決這個問題。無法解析android中的符號setOnClickListener

import android.support.v7.app.AppCompatActivity; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

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

    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.activity_main); 
} 
// Find the View that shows the numbers category 
TextView numbers = (TextView) findViewById(R.id.numbers); 

// Set a click listener on that View 
numbers.setOnClickListener(new View.OnClickListener() { 
    // The code in this method will be executed when the numbers View is clicked on. 
    @Override 
    public void onClick(View view) { 
     Intent numbersIntent = new Intent(MainActivity.this, Numbers.class); 
     startActivity(numbersIntent); 
    } 
} 

}

+4

移動你的代碼插件ide'onCreate()'。 – earthw0rmjim

+0

是或實現'OnClickListener'和'numbers.setOnClickListener(this);' – Amg91

回答

2

你的代碼應該是onCreate方法中,嘗試:

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

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

    // Find the View that shows the numbers category 
    TextView numbers = (TextView) findViewById(R.id.numbers); 

     // Set a click listener on that View 
     numbers.setOnClickListener(new View.OnClickListener() { 
     // The code in this method will be executed when the numbers View is clicked on. 

     @Override 
     public void onClick(View view) { 
      Intent numbersIntent = new Intent(MainActivity.this, Numbers.class); 
      startActivity(numbersIntent); 
     } 
    } 
} 

瞭解更多關於活動和它們的生命週期:Activity Lifecycle

+0

在onClick方法中也有一個空指針異常。幫助我擺脫這一點。 –

1

你onClickListener需要在您的onCreate方法。

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 
     // Find the View that shows the numbers category 
     TextView numbers = (TextView) findViewById(R.id.numbers); 

     // Set a click listener on that View 
     numbers.setOnClickListener(new View.OnClickListener() { 
     // The code in this method will be executed when the numbers View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent numbersIntent = new Intent(MainActivity.this, Numbers.class); 
       startActivity(numbersIntent); 
      } 
    }); 
    } 
} 

這應該修復錯誤。另外,請確保您已在您的AndroidManifest中添加了Numbers.class,否則當您啓動意圖時會出現另一個錯誤。

<activity 
     android:name=".Numbers" 
     android:label="Numbers"/> 
+0

爲什麼它在方法「numers.setOnClickListener(......)」上創建一個空指針異常? –

+0

activity_main.xml中的textview是「數字」嗎? – LBJ33

+0

是的,它在activity_main.xml中 –

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

    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.activity_main); 

    // Find the View that shows the numbers category 
    TextView numbers = (TextView) findViewById(R.id.numbers); 

    // Set a click listener on that View 
    numbers.setOnClickListener(new View.OnClickListener() { 
    // The code in this method will be executed when the numbers View is clicked on. 

    @Override 
    public void onClick(View view) { 
     Intent numbersIntent = new Intent(MainActivity.this, Numbers.class); 
     startActivity(numbersIntent); 
    } 
} 

或:

public class MainActivity extends AppCompatActivity implements OnClickListener{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the View that shows the numbers category 
     TextView numbers = (TextView) findViewById(R.id.numbers); 

     // Set a click listener on that View 
     numbers.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     Intent numbersIntent = new Intent(MainActivity.this, Numbers.class); 
     startActivity(numbersIntent); 
    } 
}