2016-07-19 9 views
-1

クリックすると別のインテントが表示されるボタンを作成したいと思います。setOnClickListenerエラー - シンボルを解決できません

これはコードです:

<Button 
    android:text="Bine ati venit!" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/button1" 
    android:background="#479fc7" 
    android:textSize="40dp" 
    android:textColor="@color/colorPrimaryDark" 
    android:textStyle="normal|bold|italic" 
    android:textAllCaps="false" 
    android:fontFamily="casual" 
    android:onClick="next_page"/> 

との.javaクラスから:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 
} 

Button button = (Button) findViewById(R.id.button1); 

button.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick (View v){ 
    next_page(v); 
} 
}); 

public void next_page(View v) { 
    Intent intent = new Intent(this, MapsActivity.class); 
    startActivity(intent); 
} 

私はエラーを取得: は、 "シンボルを解決できません 'setOnClickListener'"、「変数のonClickは決してありません";"、 "expected" ...

+5

を、それを使用しない場合はnext_page()にViewオブジェクトを渡す必要はありません'onCreate'メソッドの中の' button'ですか? –

+2

xmlファイルにすでにandroid:onClick = "next_page"というタグを設定しているので、ボタンのinitを削除でき、onClickListener –

+0

ああ、私はそれを見ていない... thx! – rocko

答えて

0

xmlのメソッド名でonClick属性を宣言するか、onCreateメソッドで設定します。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 

    Button button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick (View v){ 
      next_page(v); 
     } 
    }); 
} 

public void next_page(View v) { 
    Intent intent = new Intent(this, MapsActivity.class); 
    startActivity(intent); 
} 
0

あなたはこの

Button button; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 
    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener(){ 
     public void onClick (View v){ 
      next_page(v); 
     } 
    }); 

} 

public void next_page(View v) { 
    Intent intent = new Intent(this, MapsActivity.class); 
    startActivity(intent); 
} 

のようにコードを変更する必要があり、あなたはなぜあなたは入れていませんが

関連する問題