Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建全局复选框并在方法中使用时出错_Android_Class_Checkbox_Mainclass - Fatal编程技术网

Android 创建全局复选框并在方法中使用时出错

Android 创建全局复选框并在方法中使用时出错,android,class,checkbox,mainclass,Android,Class,Checkbox,Mainclass,我试图使复选框对象成为全局对象,以避免多次在methodes中创建它的findViewByid,它在应用程序中给出FC,在logcat中说,创建复选框的行就是有错误的行 这是它的分类代码 public class AdvJustJava_app extends AppCompatActivity { public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox); public final CheckBox

我试图使复选框对象成为全局对象,以避免多次在methodes中创建它的findViewByid,它在应用程序中给出FC,在logcat中说,创建复选框的行就是有错误的行

这是它的分类代码

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.access$800(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.access$800(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) 
02-12 19:50:53.225 17044-17044/courses.omy.dasser.androidcourses E/AndroidRuntime:致命异常:main
过程:courses.omy.dasser.androidcourses,PID:17044
java.lang.RuntimeException:无法实例化活动组件信息{courses.omy.dasser.AndroidCoures/courses.omy.dasser.AndroidCoures.AdvJustJava_app}:java.lang.NullPointerException
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
位于android.os.Handler.dispatchMessage(Handler.java:110)
位于android.os.Looper.loop(Looper.java:193)
位于android.app.ActivityThread.main(ActivityThread.java:5333)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:824)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
在dalvik.system.NativeStart.main(本机方法)
原因:java.lang.NullPointerException
在android.app.Activity.findviewbyd(Activity.java:1903)上
在courses.omy.dasser.androidcourses.AdvJustJava_app.(AdvJustJava_app.java:21)
位于java.lang.Class.newInstanceImpl(本机方法)
位于java.lang.Class.newInstance(Class.java:1215)
位于android.app.Instrumentation.newActivity(Instrumentation.java:1061)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2276)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
位于android.os.Handler.dispatchMessage(Handler.java:110)
位于android.os.Looper.loop(Looper.java:193)
位于android.app.ActivityThread.main(ActivityThread.java:5333)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:824)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
在dalvik.system.NativeStart.main(本机方法)

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

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

并在
setContentView(R.layout.adv\u just\u java\u应用程序)
调用
onCreate()
后添加这些行:


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

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

并在
setContentView(R.layout.adv\u just\u java\u应用程序)
调用
onCreate()
后添加这些行:


你不应该这样做的

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

你不应该这样做的

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

谢谢你的描述,我会在7分钟内按照stacksoverflow规则接受你的回答,15分钟后我会