簡體   English   中英

Android:使用按鈕點擊意圖啟動活動

[英]Android: starting activity using intent on button click

我正在嘗試使用intent啟動activitiy:

    public class Bez_provjere_739 extends Activity { 
Button Tbutun;
 public void onCreate(Bundle savedInstanceState) {

    Tbutun.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent otvoriIn = new Intent("com.riteh.HL.IN");

            startActivity(otvoriIn);
        }
    });

我以相同的方式在我的應用程序中多次使用它,但僅在這種情況下我得到錯誤:

05-17 00:44:43.694:E / AndroidRuntime(3533):java.lang.RuntimeException:無法啟動活動ComponentInfo {com.riteh.HL / com.riteh.HL.Bez_provjere_739}:java.lang.NullPointerException

您從未初始化按鈕也未分配給它,因此它為空。

此外,您的代碼中缺少一些基礎知識:

// naming conventions - variable names start with lower case letter
private Button tbutun; 

@Override
public void onCreate(Bundle savedInstanceState) {
    // always call super.onCreate
    super.onCreate(savedInstanceState);
    // if you have a layout XML, use it
    setContentView(R.layout.lay_xml_name);
    // if the button declared in the layout, refer to it
    tbutun = (Button)findViewById(R.id.the_button_name);
    // the rest
}

初始化你的按鈕,試試這個

  public class Bez_provjere_739 extends Activity { 
    Button Tbutun;
     public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main);

    Tbutun=(Button)findViewById(R.id.button1) //change the id as per yours

        Tbutun.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent otvoriIn = new Intent("com.riteh.HL.IN");

                startActivity(otvoriIn);
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM