尝试使用复选框发送意图时,应用在启动时崩溃

时间:2018-10-03 02:16:19

标签: android android-intent

我正在尝试为一个项目创建一个应用程序,该应用程序基于5个复选框发送意图。这些复选框有条件查找在我的第二个活动中显示之前选中的复选框总数。

我的问题是我的代码可以正常运行,但是在启动时立即崩溃。我尝试注释掉某些部分以隔离导致它的原因,但没有成功。

我刚开始从事android开发,所以我确定我做错了一些简单的事情。

    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;
}

}

崩溃日志:

  

10-03 02:22:28.868 5566-5566 / com.example.cptbetty.dailymichaelmassard E / Android运行时:致命异常:主要       流程:com.example.cptbetty.dailymichaelmassard,PID:5566       java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.cptbetty.dailymichaelmassard / com.example.cptbetty.dailymichaelmassard.DailyMichaelMassardActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android。在空对象引用上的view.Window.findViewById(int)'           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2832)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1797)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6642)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.Window.findViewById(int)”           在android.app.Activity.findViewById(Activity.java:2654)           在com.example.cptbetty.dailymichaelmassard.DailyMichaelMassardActivity。(DailyMichaelMassardActivity.java:11)           在java.lang.Class.newInstance(本地方法)           在android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)           在android.app.Instrumentation.newActivity(Instrumentation.java:1215)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2820)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1797)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6642)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

活动发送意向:

    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);
}

}

1 个答案:

答案 0 :(得分:1)

此处有误>>> CheckBox checkBox4 = findViewById(R.id.checkBox4);

之后移动

setContentView

您尝试先找到视图,然后再加载视图... 因此,解决方案是

DECLARE变量,并将其分配给onCreate之后,setContentView之后