程序闪退问题

来源: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回答

好帮手慕雪

2020-08-11

http://img.mukewang.com/climg/5f32646f09ec762c05390298.jpg

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

http://img.mukewang.com/climg/5f326495094a552e04070273.jpg

祝:学习愉快

0

慕沐2533184

提问者

2020-08-10

程序如下,app打开后闪退,无报错,请问是哪里出错了?

0

慕沐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>

0

慕沐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>

0

0 学习 · 2907 问题

查看课程