微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我无法引用我的活动中的内容,例如按钮和无线电组

如何解决我无法引用我的活动中的内容,例如按钮和无线电组

所以我试图创建一个活动,然后向它添加 onclick 侦听器,但它不会让我参考。所以当我在我的应用程序中打开这个活动时,我的应用程序崩溃了,在一个空对象引用上说“void android.widget.Button.setonClickListener(android.view.View$OnClickListener)”

我不知道为什么会这样。我也根据 xml 文件正确命名了它们。

请帮忙。

public class SubtaskActivity extends AppCompatActivity {

    EditText etSubtaskName;
    Button btnDone;
    RadioGroup radgrpPri,radgrpTime;
    RadioButton radbtnPriHigh,radbtnPriMed,radbtnPriLow,radbtnTimeMore,radbtnTimeMed,radbtnTimeLess;
    boolean priHigh,priMed,priLow,timeMore,timeMed,timeLess;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnDone = findViewById(R.id.btnDone);
        radgrpPri = findViewById(R.id.radgrpPri);
        radgrpTime = findViewById(R.id.radgrpTime);
        radbtnPriHigh = findViewById(R.id.radbtnPriHigh);
        radbtnPriMed = findViewById(R.id.radbtnPriMed);
        radbtnPriLow = findViewById(R.id.radbtnPriLow);
        radbtnTimeMore = findViewById(R.id.radbtnTimeMore);
        radbtnTimeMed = findViewById(R.id.radbtnTimeMed);
        radbtnTimeLess = findViewById(R.id.radbtnTimeLess);
        etSubtaskName = findViewById(R.id.etSubtaskName);


        radgrpPri.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {
            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {

                if (radbtnPriHigh.isChecked())
                {
                    priHigh = true;
                    priLow = false;
                    priMed = false;
                }
                else if (radbtnPriMed.isChecked())
                {
                    priHigh = false;
                    priLow = false;
                    priMed = true;
                }
                else if (radbtnPriLow.isChecked())
                {
                    priHigh = false;
                    priLow = true;
                    priMed = false;
                }

            }
        });

        radgrpTime.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {
            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {

                if (radbtnTimeMore.isChecked())
                {
                    timeMore = true;
                    timeMed = false;
                    timeLess = false;
                }
               else if (radbtnTimeMed.isChecked())
                {
                    timeMore = false;
                    timeMed = true;
                    timeLess = false;
                }
               else if (radbtnTimeLess.isChecked())
                {
                    timeMore = false;
                    timeMed = false;
                    timeLess = true;
                }
            }
        });




        btnDone.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               String name = etSubtaskName.getText().toString().trim();

               Intent intent = new Intent(SubtaskActivity.this,TaskInfo.class);
               intent.putExtra("subtaskName",name);
               intent.putExtra("priHigh",priHigh);
               intent.putExtra("priMed",priMed);
               intent.putExtra("priLow",priLow);
               intent.putExtra("timeMore",timeMore);
               intent.putExtra("timeMed",timeMed);
               intent.putExtra("timeLess",timeLess);
               startActivity(intent);




            }
        });





    }

}

XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/it"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:background="@color/background"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvSubtaskPriorityheading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="@font/roboto"
            android:text="@string/priority_of_subtask"
            android:textColor="#B8AEAE"
            android:textSize="16sp" />

        <RadioGroup
            android:id="@+id/radgrpPri"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radbtnPriHigh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:buttonTint="@color/red"
                android:text="@string/high"
                android:textColor="@color/white" />

            <RadioButton
                android:id="@+id/radbtnPriMed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/yellow"
                android:text="@string/medium"
                android:textColor="@color/white" />

            <RadioButton
                android:id="@+id/radbtnPriLow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/green"
                android:text="@string/low"
                android:textColor="@color/white" />
        </RadioGroup>

        <TextView
            android:id="@+id/tvTimeWeightheading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="@font/roboto"
            android:text="@string/time_this_subtask_may_consume"
            android:textColor="#B8AEAE"
            android:textSize="16sp" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/floating_hint_time_minutes"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:hintTextAppearance="@style/FlotatingHintStyle">


            <EditText
                android:id="@+id/etSubtaskName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:ems="10"
                android:fontFamily="@font/roboto"
                android:hint="@string/name_your_subtask"
                android:inputType="textPersonName"
                android:maxLength="20"
                android:textColor="@color/white"
                android:textColorHint="#B8AEAE"
                android:textSize="14sp" />

        </com.google.android.material.textfield.TextInputLayout>

        <RadioGroup
            android:id="@+id/radgrpTime"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radbtnTimeMore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:buttonTint="@color/red"
                android:text="@string/more"
                android:textColor="@color/white" />

            <RadioButton
                android:id="@+id/radbtnTimeMed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/yellow"
                android:text="@string/medium"
                android:textColor="@color/white" />

            <RadioButton
                android:id="@+id/radbtnTimeLess"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/green"
                android:text="@string/less"
                android:textColor="@color/white" />

        </RadioGroup>
    </LinearLayout>

    <Button
        android:id="@+id/btnDone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginRight="16dp"
        android:gravity="center_horizontal"
        android:text="@string/done"
        app:backgroundTint="@color/orange_accent" />


</LinearLayout>

解决方法

您没有调用 onCreate 方法 setContentView(R.layout.youractivity)。如果没有,Android 不知道要呈现什么,因此您无法提供任何视图。

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.youractivity);
}
,

只需将您的 (xml) 布局文件设置为 setContentView 中的 onCreate()

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?