Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多个按钮上设置一个onClickListener的问题_Java_Android_Android Studio - Fatal编程技术网

Java 在多个按钮上设置一个onClickListener的问题

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

我的布局上有3个按钮。我曾尝试在它们上设置一键式侦听器,但我的应用程序启动时崩溃。 Android Studio中没有警告。 首先,我创建了一个新的Click listener-ButtonsClick listener。然后使用开关,我为每个按钮分离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;
            }
        }
    };
}
不能在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;
    }
}
};
 }