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

时间:2016-02-12 17:49:52

标签: android class checkbox mainclass

我正在尝试将CheckBox对象设置为全局,以便不多次在methodes中创建它的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) 

2 个答案:

答案 0 :(得分:2)

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)的{​​{1}}电话后添加以下这些行:

onCreate()

答案 1 :(得分:1)

你不应该在

中这样做
<book>

相反,请执行此操作 - &gt;

from __future__ import division

import numpy as np
from PIL import Image
from bokeh.plotting import figure, show, output_file


# Open image, and make sure it's RGB*A*
lena_img = Image.open('Lenna_rect.png').convert('RGBA')
xdim, ydim = lena_img.size
print("Dimensions: ({xdim}, {ydim})".format(**locals()))
# Create an array representation for the image `img`, and an 8-bit "4
# layer/RGBA" version of it `view`.
img = np.empty((ydim, xdim), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((ydim, xdim, 4))
# Copy the RGBA image into view, flipping it so it comes right-side up
# with a lower-left origin
view[:,:,:] = np.flipud(np.asarray(lena_img))

# Display the 32-bit RGBA image
dim = max(xdim, ydim)
fig = figure(title="Lena",
             x_range=(0,dim), y_range=(0,dim),
             # Specifying xdim/ydim isn't quire right :-(
             # width=xdim, height=ydim,
             )
fig.image_rgba(image=[img], x=0, y=0, dw=xdim, dh=ydim)

output_file("lena.html", title="image example")

show(fig)  # open a browser