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

达瑟·巴斯尤尼(Dasser Basyouni)

我试图使该CheckBox对象成为全局对象,以便不多次在方法中创建它的findViewByid,并且它在应用程序中提供了FC,而在logcat中则说创建CheckBox的那一行是有错误的那一行

这是课程代码

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) 
常用软件

您必须findViewById()等到之后才能打电话setContentView()代替:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何创建全局变量并在其他页面中使用它?

如何为会话创建全局变量并在测试用例中使用它(RobotFramework、REST API)

Django:服务器启动时创建对象并在视图中使用它

创建全局Toast方法以在所有活动中使用它吗?

如何创建方法并在另一个类中使用它?

如何创建对象并在Objective-C中使用它?

如何创建服务并在控制器中使用它

JavaScript:异步创建实例并在不同模块中使用它

创建自己的异常并在C#中使用它

创建我的模型Jobs的DropDownList并在CreateEmployeesViewModel中使用它

在函数中创建字典并在子中使用它

如何声明全局变量并在函数中使用它们 [Visual Basic]

如何声明结构的全局数组并在不同的函数中使用它?

在 NativeScript 中使用 JS 创建自定义类并在原生 Android 中使用它

如何在扩展类中使用方法并在主类中使用它们

检查NaN并在If中使用它

如何创建签名证书并在生产中的IdentityServer4中使用它?

创建可执行的COM作为ATL项目并在C#中使用它

如何在oracle sql脚本中创建过程并在脚本中使用它?

如何在代码中创建自定义UIButton并在xib中使用它?

是否可以从JRuby创建Java类并在Java中使用它们?

如何创建Gdk :: Pixbuf实例的std :: map并在Gdk :: Cairo中使用它

如何正确创建一个单例对象并在C ++中使用它?

如何从Haxe创建iOS和OSX库并在本机应用程序中使用它?

在函数中创建数组,并在另一个函数中使用它

C ++,Gnuplot:在RAM中创建临时文件并在Gnuplot中使用它

JavaScript-从json对象创建文件并在FormData中使用它

是否可以创建react组件并在其他项目html中使用它?

通过 html 按钮创建变量并在外部函数中使用它