创建全局 CheckBox 并在方法中使用它时出错

Error Creating global CheckBox and using it in a method

我正在尝试使该 CheckBox 对象成为全局对象,以免在方法中多次创建它的 findViewByid 并且它在应用程序中提供 FC,在 logcat 中表示创建 CheckBox 的行是行出现错误

这是class代码

public class AdvJustJava_app extends AppCompatActivity {

public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adv_just_java_app);
    Button DecBtn = (Button) findViewById(R.id.adv_dec_btn);
    DecBtn.setEnabled(false);
}

int Q=0, P=0, WC=0, C=0;


private void adv_displayQuantity(int numberOfCoffees) {
    TextView quantityTextView = (TextView) findViewById(R.id.adv_quantity_text_view);
    quantityTextView.setText("" + numberOfCoffees);
}

private void adv_displayOrderSummary(String message) {
    TextView quantityTextView = (TextView) findViewById(R.id.adv_order_summary_text_view);
    TextView ThanksTextView = (TextView) findViewById(R.id.adv_thank_you_tv);
    ThanksTextView.setText("Thank You ;)");

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ThanksTextView.getLayoutParams();
    params.setMargins(0, 4, 0, 4    ); //substitute parameters for left, top, right, bottom
    ThanksTextView.setLayoutParams(params);

    quantityTextView.setText(message);
}

public void adv_OrderSummary(View view) {
    if(Q==0) {
        if(!checkBoxWC.isChecked()){WC = 0;}
        if(!checkBoxC.isChecked()){C = 0;}
        P= WC + C ;
        adv_displayOrderSummary("Free :D");
        Toast toast = Toast.makeText(this, "Your Ordered Quantity is Zero", Toast.LENGTH_LONG);
        toast.show();
    }else if(!checkBoxWC.isChecked()&& !checkBoxC.isChecked() ){
            adv_displayOrderSummary("Free :D");
            Toast toast = Toast.makeText(this, "You Did Not Select the Product", Toast.LENGTH_LONG);
            toast.show();
    }else {
        EditText editText = (EditText)findViewById(R.id.nameET);
        String name = (String) editText.getText().toString();
        String adv_Price = NumberFormat.getCurrencyInstance().format(P);
        adv_displayOrderSummary("Name: " + name + "\nQuantity: " + Q + "\nTotal: " + adv_Price);
    }
 }
}

也就是Logcat

02-12 19:50:53.225 17044-17044/courses.omy.dasser.androidcousres E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: courses.omy.dasser.androidcousres, PID: 17044
                                                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{courses.omy.dasser.androidcousres/courses.omy.dasser.androidcousres.AdvJustJava_app}: java.lang.NullPointerException
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:151)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                   at android.os.Looper.loop(Looper.java:193)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.NullPointerException
                                                                                   at android.app.Activity.findViewById(Activity.java:1903)
                                                                                   at courses.omy.dasser.androidcousres.AdvJustJava_app.<init>(AdvJustJava_app.java:21)
                                                                                   at java.lang.Class.newInstanceImpl(Native Method)
                                                                                   at java.lang.Class.newInstance(Class.java:1215)
                                                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2276)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:151) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                   at android.os.Looper.loop(Looper.java:193) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 

setContentView() 之后才能调用 findViewById()。替换:

public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

与:

public CheckBox checkBoxWC;
public CheckBox checkBoxC;

并在 setContentView(R.layout.adv_just_java_app) 调用 onCreate() 后添加这些行:

checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

你不应该在

中这样做
public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

改为这样做 ->

public CheckBox checkBoxWC;
public CheckBox checkBoxC;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adv_just_java_app);
    checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
    checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);
    Button DecBtn = (Button) findViewById(R.id.adv_dec_btn);
    DecBtn.setEnabled(false);
}