Android Studio 0.3.1 button.setOnClickListener(this);崩溃的应用程序

时间:2013-10-31 16:06:06

标签: android-studio onclicklistener

我有以下代码在Eclispe下正常运行但在Android Studio下仍然崩溃。

public class MainActivity extends Activity implements OnClickListener{

private Button playBtn = null;

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

        playBtn = (Button)findViewById(R.id.play_btn);
        playBtn.setOnClickListener(this);
    }

   ....

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.play_btn){
           //play button
        }
    }

代码在到达OnCreate()时崩溃。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

检查出来..

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

Button normal;

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

     normal=(Button)findViewById(R.id.btn_normal);
     normal.setOnClickListener(this);
}

@Override
public void onClick(View view) {

switch (view.getId())
{
   case R.id.btn_normal:

    //Do what you want on clicking on the button.

   break;
 }
 }
}

对我有用..