2016-09-10 7 views
-1

「setOnClickListener」の下に赤い線が表示されると、「setOnClickListenerを解決できません」というポップアップメッセージが表示され、「@Override」に「注釈はここでは許可されていません」と表示されます。 「vを見る」のvも私にエラーを与えています。私はどこでうんざりしたのですか? onClickListenerを設定するための注文方法などonCreate内にコードを移動する必要私はエラーが発生した私のsetOnClickListenerですか?

答えて

2

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

public class EmailReceiptActivity extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_email_receipt); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main_options_menu, menu); 
    menu.findItem(R.menu.main_options_menu).setIntent(
      new Intent(EmailReceiptActivity.this, LaunchActivity.class)); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    //use the id of the item from main_options_menu 
    if (id == R.id.logoff_menu_item) { 
     super.onOptionsItemSelected(item); 
     startActivity(item.getIntent()); 
    } 

    return true; 
} 

Button btn_send = (Button)findViewById(R.id.send_receipt_button); 
btn_send.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    //Use the name of the function you assigned to the xml design of the button 
    public void onClick(View v){ 
     //Use the name of this class, and the name class where you want to be taken when the button is clicked. 
     startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class)); 
    } 
} 

}。関数本体の外でそのようなコードを実行することはできません。また、それは... onCreateの外に、よりグローバルスコープを使用してButtonを宣言し、そうしようとするよりも有用である

public Button btn_send; 

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

    btn_send = (Button)findViewById(R.id.send_receipt_button); 
    btn_send.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     //Use the name of the function you assigned to the xml design of the button 
     public void onClick(View v){ 
      //Use the name of this class, and the name class where you want to be taken when the button is clicked. 
      startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class)); 
    } 
} 
+0

これで解決しました。どうもありがとうございます! – agentmg123

0

プライベートボタンsend_receipt_button。

public class GameOver extends AppCompatActivity implements View.OnClickListener { 

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

      setContentView(R.layout.activity_email_receipt); 
       Button btn_send = (ImageButton) findViewById(R.id.send_receipt_button); 
      btn_send.setOnClickListener(this); 

} 

    public void onClick (View v){ 



      switch (v.getId()) { 

       case R.id.send_receipt_button: 
        startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class)); 


        break; 
} 
} 
+0

この回答は元の内容と同じように見えますが、ここで私が少し答えを出したことを知っておくべきです(http://stackoverflow.com/questions/39429490/pass-image-array-from-view-did-load -to-tableview-cell?noredirect = 1#comment66182594_39429490)これを削除することは、ここからの帰属なしにコピーされているため、正しいことでした。http://stackoverflow.com/a/25081954/294949これは剽窃とみなされ、ここでは非常に眉をひそめており、(見て分かるように)発見するのは簡単です。 – danh

+0

これは私のアプリケーションで使用している私のオリジナルコードです。そして、存在する答えは盗用を助け、欺くことではなく、盗用は1つのブログやサービスwikiに投稿されるものではありません。 –

+0

あなたの動機が他の人を助けることであると確信しています。誰か他の人の答えを示す正しい方法は、*リンク*することです。読者がその一部をコピーする方が意味がある場合は、ソースを引用してください。 – danh

2

あなたがそのように外にあなたのOnClickListenerを入れたい場合は、それは次のようにする必要があります:

OnClickListener sendListener = new View.OnClickListener(){ 
    @Override 
    //Use the name of the function you assigned to the xml design of the button 
    public void onClick(View v){ 
     //Use the name of this class, and the name class where you want to be taken when the button is clicked. 
     startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class)); 
    } 
}; 

、あなたはonCreate

Button btn_send; 
protected void onCreate(Bundle savedInstanceState) { 
    ... 

    btn_send.setOnClickListener(sendListener); 
} 

のような領域にそれを設定しますが、理想的findViewByIdOnClickListenerの両方をonCreate

のような領域に移動する場合は、
Button btn_send; 
protected void onCreate(Bundle savedInstanceState) { 
    ... 

    btn_send = (Button)findViewById(R.id.send_receipt_button); 
    btn_send.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     //Use the name of the function you assigned to the xml design of the button 
     public void onClick(View v){ 
      //Use the name of this class, and the name class where you want to be taken when the button is clicked. 
      startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class)); 
    } 
} 
関連する問題