2016-09-02 10 views
-4

私はこのプログラムを実行すると、 "シンボルsetOnClickListenerを解決できません"というメッセージが繰り返し表示されます。は、アンドロイドのシンボル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

移動

@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); } } } 

活動とそのライフサイクルについてもっと読みますあなたのコードインide 'onCreate()' – earthw0rmjim

+0

はい、または、「OnClickListener」と「numbers.setOnClickListener(this);」を実装します。 – Amg91

答えて

2

あなたのコードは、試すonCreateメソッド内でなければなりません:Activity Lifecycle

+0

また、これを取り除くためにonClick method.help meにnullポインタ例外があります。 –

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(......)」メソッドでnullポインタ例外を作成するのはなぜですか? –

+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); 
    } 
} 
関連する問題