RadioButton符号未在Android Studio中解析

时间:2015-12-20 12:40:45

标签: android

希望这不是一个愚蠢的问题,但我的java代码有问题,我希望你们能找到我的错误。

我在malebutton上找到了一个“无法解析符号变量”,在femaleSelected所在的底部。我在onCreate中定义了单选按钮。我认为onCreate受到保护,这就是我无法访问按钮的原因。但将其改为公开解决方案。

这是完整的源代码..

package ideaman924.playground;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {


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

        RadioButton malebutton,femalebutton;
        malebutton = (RadioButton) findViewById(R.id.male);
        femalebutton = (RadioButton) findViewById(R.id.female);
    }

    @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);
    }

    public void femaleSelected(View view) {
        malebutton.toggle(); //this is where I'm getting the error
        //but only on the malebutton, not the toggle
    }

}

知道为什么会这样吗?

编辑:这里也是xml布局。它在韩国,但它很简单(男性和女性)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="성별 선택" />

    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="여성"
        android:onClick="femaleSelected" />

    <RadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="남성"
        android:onClick="maleSelected" />



</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您的RadioButton malebutton本地变量,仅在该方法中可见。

如果你想在班级看到它,你应该移动

    RadioButton malebutton,femalebutton;

public class MainActivity extends AppCompatActivity {

了解variables

答案 1 :(得分:0)

您需要在类的onCreate方法全局变量中定义RadioButton malebutton。

private static MsgTree buildNextMessageTree() {
    char c = encodingString.charAt(staticCharIdx++);
    MsgTree tree = new MsgTree(c);
    if (c == '^') {
        tree.left = buildNextMessageTree();
        tree.right = buildNextMessageTree();
    }
    return tree;
}