javaandroidxmlforceclose

Force Close in the android app


Android Studio

hi guys.

I wanted to make a simple app for android which uses <<OnCheckedChangeListener>> for a check box in android studio. I looked at the questions which were like mine for example link, but I found no problem in my codes. These Are Mine. Thanks a lot in advance.

This Is My MainActivity.java

package com.example.gna.myapplication;
  
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends Activity {

    public CheckBox chbox=(CheckBox)findViewById(R.id.CH1);
    public TextView txt1 =(TextView)findViewById(R.id.TXT1);

    CompoundButton.OnCheckedChangeListener list=new       CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
                txt1.setText("A New Era");
            else
                txt1.setText("MasterByte");

        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState){ 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chbox.setOnCheckedChangeListener(list);
    }
}

And The XML is:

activity_main.Xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android" xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/rl">

    <TextView android:text="MasterByte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:textColor="#F00FF0"
        android:id="@+id/TXT1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Here"
        android:id="@+id/CH1"
        android:textSize="30dp"
        android:layout_below="@+id/TXT1"
        android:layout_alignRight="@+id/TXT1"
        android:layout_alignEnd="@+id/TXT1"
        android:layout_marginTop="55dp" />

</RelativeLayout>

OKAY. My problem is that the app won't start and it shows force close error. I wanted to use exception classes and [try(),catch(),finally()] but I didn't know how . If you Know how to do That , I'm waiting :D Sorry for my bad English!


Solution

  • move the initialization of

     public CheckBox chbox=(CheckBox)findViewById(R.id.CH1);
     public TextView txt1 =(TextView)findViewById(R.id.TXT1);
    

    inside onCreate and after setContentView. You need a valid Context and something to look for before being able to actually find something

    My problem is that the app won't start and it shows force close error.

    you can't use findViewById before the Activity goes for its lifecycle.