Java 在多个按钮上设置一个onClickListener的问题
我的布局上有3个按钮。我曾尝试在它们上设置一键式侦听器,但我的应用程序启动时崩溃。 Android Studio中没有警告。 首先,我创建了一个新的Click listener-ButtonsClick listener。然后使用开关,我为每个按钮分离exe代码。在Java 在多个按钮上设置一个onClickListener的问题,java,android,android-studio,Java,Android,Android Studio,我的布局上有3个按钮。我曾尝试在它们上设置一键式侦听器,但我的应用程序启动时崩溃。 Android Studio中没有警告。 首先,我创建了一个新的Click listener-ButtonsClick listener。然后使用开关,我为每个按钮分离exe代码。在onCreate方法中,我将侦听器设置为按钮 public class MainActivity extends ActionBarActivity { private RelativeLayout mRelativeLay
onCreate
方法中,我将侦听器设置为按钮
public class MainActivity extends ActionBarActivity {
private RelativeLayout mRelativeLayout;
private TextView mInfoTextView;
Button buttonSetBackgroundRed =(Button) findViewById(R.id.buttonSetBackgroundRed);
Button buttonSetBackgroundYellow =(Button) findViewById(R.id.buttonSetBackgroundYellow);
Button buttonSetBackgroundGreen =(Button) findViewById(R.id.buttonSetBackgroundGreen);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
mInfoTextView = (TextView) findViewById(R.id.infoTextView);
buttonSetBackgroundRed.setOnClickListener(buttonsClickListener);
buttonSetBackgroundYellow.setOnClickListener(buttonsClickListener);
buttonSetBackgroundGreen.setOnClickListener(buttonsClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
View.OnClickListener buttonsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonSetBackgroundRed:
mInfoTextView.setText(R.string.set_background_red);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));
break;
case R.id.buttonSetBackgroundYellow:
mInfoTextView.setText(R.string.set_background_yellow);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.yellowColor));
break;
case R.id.buttonSetBackgroundGreen:
mInfoTextView.setText(R.string.set_background_green);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.greenColor));
break;
}
}
};
}
不能在onCreate()方法之外使用findViewById(),因为在调用onCreate()之前甚至没有创建视图。所以,找不到任何视图。将代码更改为以下内容
public class MainActivity extends ActionBarActivity {
private RelativeLayout mRelativeLayout;
private TextView mInfoTextView;
Button buttonSetBackgroundRed;
Button buttonSetBackgroundYellow;
Button buttonSetBackgroundGreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSetBackgroundRed =(Button) findViewById(R.id.buttonSetBackgroundRed);
buttonSetBackgroundYellow =(Button) findViewById(R.id.buttonSetBackgroundYellow);
buttonSetBackgroundGreen =(Button) findViewById(R.id.buttonSetBackgroundGreen);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
mInfoTextView = (TextView) findViewById(R.id.infoTextView);
buttonSetBackgroundRed.setOnClickListener(buttonsClickListener);
buttonSetBackgroundYellow.setOnClickListener(buttonsClickListener);
buttonSetBackgroundGreen.setOnClickListener(buttonsClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
View.OnClickListener buttonsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonSetBackgroundRed:
mInfoTextView.setText(R.string.set_background_red);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));
break;
case R.id.buttonSetBackgroundYellow:
mInfoTextView.setText(R.string.set_background_yellow);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.yellowColor));
break;
case R.id.buttonSetBackgroundGreen:
mInfoTextView.setText(R.string.set_background_green);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.greenColor));
break;
}
}
};
}
尝试在xml文件中使用按钮的onClick属性,并在代码中对其进行如下调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn"
android:text="Button" />
</LinearLayout>
这样,您的代码也将减少….您不能在方法之外调用
findViewById()
。将这些调用移到onCreate()
之后的setContentView()
中。哦,谢谢,我没有多少理论知识)。有必要在问题评分上设置-1吗?我只想说我正在进步,学习和诸如此类的东西……是的,迈克说的是真的。当视图尚未创建时,您正在调用findViewById,因此无论何时尝试将onClickListener设置为对象,您都应该得到NullPointerException。最好使用旧方法。Bkz新的一个在片段中不起作用。但事实上这也是答案
public class MainActivity extends ActionBarActivity {
private RelativeLayout mRelativeLayout;
private TextView mInfoTextView;
Button buttonSetBackgroundRed =(Button)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.buttonSetBackgroundRed);
Button buttonSetBackgroundYellow =(Button) findViewById(R.id.buttonSetBackgroundYellow);
Button buttonSetBackgroundGreen =(Button) findViewById(R.id.buttonSetBackgroundGreen);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
mInfoTextView = (TextView) findViewById(R.id.infoTextView);
buttonSetBackgroundRed.setOnClickListener(buttonsClickListener);
buttonSetBackgroundYellow.setOnClickListener(buttonsClickListener);
buttonSetBackgroundGreen.setOnClickListener(buttonsClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
View.OnClickListener buttonsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonSetBackgroundRed:
mInfoTextView.setText(R.string.set_background_red);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));
break;
case R.id.buttonSetBackgroundYellow:
mInfoTextView.setText(R.string.set_background_yellow);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.yellowColor));
break;
case R.id.buttonSetBackgroundGreen:
mInfoTextView.setText(R.string.set_background_green);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.greenColor));
break;
}
}
};
}
public class MainActivity extends ActionBarActivity {
private RelativeLayout mRelativeLayout;
private TextView mInfoTextView;
Button buttonSetBackgroundRed =(Button)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.buttonSetBackgroundRed);
Button buttonSetBackgroundYellow =(Button) findViewById(R.id.buttonSetBackgroundYellow);
Button buttonSetBackgroundGreen =(Button) findViewById(R.id.buttonSetBackgroundGreen);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
mInfoTextView = (TextView) findViewById(R.id.infoTextView);
buttonSetBackgroundRed.setOnClickListener(buttonsClickListener);
buttonSetBackgroundYellow.setOnClickListener(buttonsClickListener);
buttonSetBackgroundGreen.setOnClickListener(buttonsClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
View.OnClickListener buttonsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonSetBackgroundRed:
mInfoTextView.setText(R.string.set_background_red);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));
break;
case R.id.buttonSetBackgroundYellow:
mInfoTextView.setText(R.string.set_background_yellow);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.yellowColor));
break;
case R.id.buttonSetBackgroundGreen:
mInfoTextView.setText(R.string.set_background_green);
mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.greenColor));
break;
}
}
};
}