如何解决我无法引用我的活动中的内容,例如按钮和无线电组
所以我试图创建一个活动,然后向它添加 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 举报,一经查实,本站将立刻删除。