程序闪退问题
来源:1-1 CheckBox
慕沐2533184
2020-08-10 23:03:49
package com.example.layoutdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox checkBox=findViewById(R.id.checkBox);
checkBox.setChecked(false);
boolean isChecked=checkBox.isChecked();
Log.d(TAG, "onCreate,isChecked: "+isChecked);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: "+isChecked);
}
});
}
}
4回答

请注意,这两个id要一致。

祝:学习愉快
慕沐2533184
提问者
2020-08-10
程序如下,app打开后闪退,无报错,请问是哪里出错了?
慕沐2533184
提问者
2020-08-10
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.layoutdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
慕沐2533184
提问者
2020-08-10
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
android:checked="true"
/>
</LinearLayout>
相似问题