0

I'm trying to create an app for a project that send an intent based off of 5 check boxes. These check boxes have a conditional to find the total number of check boxes selected before being displayed on my second activity.

My problem is that my code complies just fine but crashes instantly on launch. I've tried commenting out sections to isolate whats causing it but no success.

I'm fairly new to android development so i'm sure its something simple i'm doing wrong.

    package com.example.cptbetty.dailymichaelmassard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

public class DailyMichaelMassardActivity extends 
Activity implements View.OnClickListener {

CheckBox checkBox1 = findViewById(R.id.checkBox1);
CheckBox checkBox2 = findViewById(R.id.checkBox2);
CheckBox checkBox3 = findViewById(R.id.checkBox3);
CheckBox checkBox4 = findViewById(R.id.checkBox4);
CheckBox checkBox5 = findViewById(R.id.checkBox5);
int sum = 0;

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

    checkBox1.setOnClickListener(this);
    checkBox2.setOnClickListener(this);
    checkBox3.setOnClickListener(this);
    checkBox4.setOnClickListener(this);
    checkBox5.setOnClickListener(this);
}

public void onSend(View view)
{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(StatusActivity.MSG_Title, sum);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, sum);
    startActivity(intent);

}


@Override
public void onClick(View v)
{
   sum = 0;
    if (checkBox1.isChecked())
    {
        sum += 1;
    }
    else{
        sum += 0;
    }
    if (checkBox2.isChecked())
    {
        sum +=1;
    }
    else {
        sum += 0;
    }
    if (checkBox3.isChecked())
    {
        sum += 1;
    }
    else{
        sum =+ 0;
    }
    if (checkBox4.isChecked())
    {
        sum += 1;
    }
    else{
        sum += 0;
    }
    if (checkBox5.isChecked())
    {
        sum +=1;
    }
    else {
        sum += 0;
    }
    return;
}

}

Crash Log:

10-03 02:22:28.868 5566-5566/com.example.cptbetty.dailymichaelmassard E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cptbetty.dailymichaelmassard, PID: 5566 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cptbetty.dailymichaelmassard/com.example.cptbetty.dailymichaelmassard.DailyMichaelMassardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2832) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6642) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2654) at com.example.cptbetty.dailymichaelmassard.DailyMichaelMassardActivity.(DailyMichaelMassardActivity.java:11) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at android.app.Instrumentation.newActivity(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2820) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6642)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Activity im sending intent to:

    package com.example.cptbetty.dailymichaelmassard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class StatusActivity extends Activity {
    public static final String MSG_Title = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status);
    Intent intent = getIntent();
    String s = intent.getStringExtra(MSG_Title);
    TextView completeView = findViewById(R.id.completeView);
    completeView.setText(s);
}

}

Cptbetty
  • 13
  • 4

1 Answers1

1

mistake here >>> CheckBox checkBox4 = findViewById(R.id.checkBox4);

just move it after

setContentView

you try to find view before view will loaded ... so, solution is

DECLARE variables and after assign it in onCreate, after setContentView

Artem
  • 303
  • 2
  • 9