当前位置: 代码迷 >> Android >> setContentView()后对Button设置的监听无效了,该怎么处理
  详细解决方案

setContentView()后对Button设置的监听无效了,该怎么处理

热度:81   发布时间:2016-05-01 22:25:26.0
setContentView()后对Button设置的监听无效了
setContentView()后,必须再次对相应的Button设置监听吗?




代码如下,


Java code
package lab.sodino.layoutdemo;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;public class LayoutDemoAct extends Activity implements OnClickListener {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        // Set up click listeners for all the buttons        View linearView = findViewById(R.id.linearBtn);        linearView.setOnClickListener(this);        View tableView = findViewById(R.id.tableBtn);        tableView.setOnClickListener(this);        View frameView = findViewById(R.id.frameBtn);        frameView.setOnClickListener(this);        View absoluteView = findViewById(R.id.absoluteBtn);        absoluteView.setOnClickListener(this);        View relativeView = findViewById(R.id.relativeBtn);        relativeView.setOnClickListener(this);    }    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        MenuInflater inflater = getMenuInflater();        inflater.inflate(R.menu.menu, menu);        Log.d("Sodino", "Inflate menu item");        return true;    }    public void onClick(View v) {        //点击按钮进入不同的布局界面        if (v.getId() == R.id.linearBtn) {            //进入linearLayout界面            setContentView(R.layout.linearlayoutdemo);        } else if (v.getId() == R.id.tableBtn) {            setContentView(R.layout.tablelayoutdemo);        } else if (v.getId() == R.id.frameBtn) {            setContentView(R.layout.framelayoutdemo);        } else if (v.getId() == R.id.absoluteBtn) {            setContentView(R.layout.absolutelayoutdemo);        } else if (v.getId() == R.id.relativeBtn) {            setContentView(R.layout.relativelayoutdemo);        }    }    public boolean onOptionsItemSelected(MenuItem item) {//        super.onOptionsItemSelected(item);        if (item.getItemId() == R.id.backMenu) {            setContentView(R.layout.main);            //返回 主界面 后,如果没有再次设置监听            //则再次返回主界面时点击相应的按钮无效            View linearView = findViewById(R.id.linearBtn);            linearView.setOnClickListener(this);            View tableView = findViewById(R.id.tableBtn);            tableView.setOnClickListener(this);            View frameView = findViewById(R.id.frameBtn);            frameView.setOnClickListener(this);            View absoluteView = findViewById(R.id.absoluteBtn);            absoluteView.setOnClickListener(this);            View relativeView = findViewById(R.id.relativeBtn);            relativeView.setOnClickListener(this);            return true;        }        return false;    }}


------解决方案--------------------
每次你都贴了不一样的View 应该需要再关联一下里面控件的ID的
------解决方案--------------------
findViewById与setContentView所设定的view是关联在一起的,调用setContentView之后,必须重新findViewById
  相关解决方案