我的布局上有3个按钮。我试图在它们上设置一个点击监听器,但是我的应用程序在启动时崩溃了。
Android Studio中没有警告。
首先,我创建了新的Click侦听器 - buttonsClickListener。然后使用开关I为每个按钮分隔exe代码。在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;
}
}
};
}
答案 0 :(得分:0)
您不能在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;
}
}
};
}
答案 1 :(得分:0)
尝试在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>
在你的Activity类中创建这个方法,
public void btn(View v){
//Your code on clicked
}
这样你的代码也会减少.....
答案 2 :(得分:0)
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;
}
}
};
}