我有一个FragmentActivity,用几个按钮加载一个Fragment。现在我想在FragmentActivity中注册按钮的onClick-Event。
到目前为止,这是我的代码:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new TestFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container2, fragment)
.commit();
Button button = (Button) fragment.getView().findViewById(R.id.buttonSettings);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//SettingsActivity activity = new SettingsActivity();
}
}
);
}
使用此代码,当我尝试在此行中设置OnClickListener时,应用程序崩溃:
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//SettingsActivity activity = new SettingsActivity();
}
}
);
我做错了什么?