2016-09-10 79 views
-1

當我將鼠標懸停在「setOnClickListener」下方的紅色曲線上時,彈出消息顯示「無法解析符號setOnClickListener」,「@Override」顯示「此處不允許註釋」。而「View v」中的v也給我一個錯誤。我在哪裏搞砸了?我的是我的setOnClickListener給我一個錯誤?

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

}

回答

2

如果要在代碼移動到如onCreate方法內的onClickListener進行設置。你不能在函數體外執行那樣的代碼。此外,它可能更有用一個更全局範圍來聲明ButtononCreate外面,所以儘量...

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

這個答案看起來像原創內容,但你應該知道,我GOOGLE了一點你在這裏給出的答案(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

這是我使用我的應用程序的原始代碼。並且顯示存在的答案是幫助並且不抄襲,剽竊不是發佈在一個博客或服務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); 
} 

但最好,您希望將您的findViewById和您的OnClickListener移動到一個區域,如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)); 
    } 
}