有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

所以我试图创建一个活动,然后向其中添加onclick侦听器,但它不允许我引用。所以当我在我的应用程序中打开这个活动时,我的应用程序崩溃了,说“void 安卓”。小装置。按钮setOnClickListener(安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:id="@+id/it"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_margin="16dp"
    安卓:background="@color/background"
    安卓:orientation="vertical">

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

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

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

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

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

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

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

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


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

        </com.google.安卓.material.textfield.TextInputLayout>

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

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

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

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

        </RadioGroup>
    </LinearLayout>

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


</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    你没有调用onCreate方法setContentView(R.layout.youractivity)。如果没有,Android不知道要渲染什么,因此没有视图可供您提供

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.youractivity);
    }
    
  2. # 2 楼答案

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

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