簡體   English   中英

OnClickListener Android Studio

[英]OnClickListener Android Studio

嗨,我正在嘗試為Android創建一個應用程序,但是當我嘗試使用2 setOnClickListener啟動它時,它崩潰了,實際上,如果我刪除了兩個事件之一,它不會崩潰怎么辦?

PS對不起,我的英語不好,但我是意大利語

主要活動

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that wil return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.FeedBackHome);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.contact);
        }
    });



    Button button= (Button) findViewById(R.id.NotReg);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Register.class));
        }
    });




}
  1. 第一個非法代碼:

您不能像在這里一樣設置新的Content View onClick:

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.contact); // <-- this is wrong
    }
});
  1. 確保Register.class包含布局並能夠從其他活動中打開。

暫無
暫無

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

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