-1

我正在使用一个按钮,当您单击此按钮时,将自动创建一个文本字段并且按钮状态自动禁用,但是如果您单击禁用按钮文本字段将自动删除并且按钮状态更改为启用。我正在经历这个过程。

public class ConfigurationScreen extends MainScreen implements FieldChangeListener{
    TextField tf_text;
    tf_text =  new TextField(TextField.TYPE_PLAIN,img_text[1],img_text[0],TextField.FIELD_HCENTER);
    tf_text.setWidth(Display.getWidth()/2+20);

    ImageButton btn_en;
    btn_en = new ImageButton(imgs_tmintrvl1,"enable",ImageButton.FIELD_HCENTER);

    ImageButton btn_dis;
    btn_dis=new ImageButton(imgs_tmintrvl1,"Disable",ImageButton.FIELD_HCENTER); 

    add(btn_en);

    btn_en.setChangeListener(this);

    public void fieldChanged(Field field, int context) {
        if( field==btn_en)
        {
            delete(btn_en);
            insert(btn_dis, 4);
            insert(tf_text, 5);

            System.out.println(ex);
        }else if (field == btn_dis){
            delete(btn_dis);
            delete(tf_text);
            insert(btn_en, 4);

            System.out.println("Disable Button="+ex);
        }
    }    

但是当我运行这段代码时,我得到了空指针异常,请帮助我在哪里出错。

4

1 回答 1

0
package org.maxmobility.wmp.beta;

import com.maxmobility.customfield.H_FieldManager;
import com.maxmobility.customfield.ImageButton;
import com.maxmobility.customfield.LabelField;
import com.maxmobility.customfield.PillButtonField;
import com.maxmobility.customfield.PillButtonSet;
import com.maxmobility.customfield.TextArea;
import com.maxmobility.customfield.TextField;
import com.maxmobility.util.AppColors;

import net.rim.device.api.collection.List;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class ConfigurationScreen  extends MainScreen implements FieldChangeListener{

    TextField tf_phn1;
    TextField tf_phn2; 
    TextField tf_msg; 
    TextArea ta_msg;
    ImageButton btn_add;
    ImageButton btn_subtract;
    ImageButton btn_en;
    ImageButton btn_dis;
    ImageButton btn_time1, btn_time2, btn_time3;
    LabelField lf_msg;
    LabelField lf_time;
    LabelField lf1,lf2,lf3,lf4;
    private Bitmap[] imgs_addbtn;
    private Bitmap[] imgs_subbtn;
    private Bitmap[] imgs_tmintrvl1;
    private Bitmap[] imgs_tmintrvl2;
    private Bitmap[] imgs_tmintrvl3;
    private EncodedImage[] img_text;
    private H_FieldManager hfm1 , hfm2;
    private HorizontalFieldManager hfm3,hfm4;
    private VerticalFieldManager vfm;
    private boolean show = false;
    PillButtonField pbf1;
    PillButtonField pbf2;
    PillButtonField pbf3;
    PillButtonSet pbf_set;

    public ConfigurationScreen(){
        super(USE_ALL_HEIGHT | USE_ALL_WIDTH);
        Background background = BackgroundFactory
                .createSolidBackground(AppColors.APP_BG);
        this.getMainManager().setBackground(background);

        LabelField lf = new LabelField("Configuration Screen", LabelField.FIELD_HCENTER | LabelField.USE_ALL_WIDTH);
        lf.setColor(Color.RED);
        setTitle(lf);

         img_text = new EncodedImage[2];
         img_text[0] = EncodedImage.getEncodedImageResource("edittext_selected.png");
         img_text[1] = EncodedImage.getEncodedImageResource("edittext.png");

        imgs_addbtn =  new Bitmap[3];
        imgs_addbtn[0] = Bitmap.getBitmapResource("btn_add_sub.png");
        imgs_addbtn[1] = Bitmap.getBitmapResource("btn_add_subfocused.png");
        imgs_addbtn[2] = Bitmap.getBitmapResource("btn_add_subfocused.png");

        imgs_subbtn =  new Bitmap[3];
        imgs_subbtn[0] = Bitmap.getBitmapResource("btn_add_sub.png");
        imgs_subbtn[1] = Bitmap.getBitmapResource("btn_add_subfocused.png");
        imgs_subbtn[2] = Bitmap.getBitmapResource("btn_add_subfocused.png");

        imgs_tmintrvl1 =  new Bitmap[3];
        imgs_tmintrvl1[0] = Bitmap.getBitmapResource("default_btn_Unfocused.png");
        imgs_tmintrvl1[1] = Bitmap.getBitmapResource("default_btn_focused.png");
        imgs_tmintrvl1[2] = Bitmap.getBitmapResource("default_btn_focused.png");

//        imgs_tmintrvl2 =  new Bitmap[3];
//        imgs_tmintrvl2[0] = Bitmap.getBitmapResource("default_btn_Unfocused.png");
//        imgs_tmintrvl2[1] = Bitmap.getBitmapResource("default_btn_focused.png");
//        imgs_tmintrvl2[2] = Bitmap.getBitmapResource("default_btn_focused.png");
//        
//        imgs_tmintrvl3 =  new Bitmap[3];
//        imgs_tmintrvl3[0] = Bitmap.getBitmapResource("default_btn_Unfocused.png");
//        imgs_tmintrvl3[1] = Bitmap.getBitmapResource("default_btn_focused.png");
//        imgs_tmintrvl3[2] = Bitmap.getBitmapResource("default_btn_focused.png");
//        
        tf_phn1 = new TextField(TextField.TYPE_PHONE,img_text[1],img_text[0],TextField.FIELD_LEFT);
        tf_phn2 = new TextField(TextField.TYPE_PHONE,img_text[1],img_text[0],TextField.FIELD_LEFT);
        tf_msg = new TextField(TextField.TYPE_PLAIN,img_text[1],img_text[0],TextField.FIELD_HCENTER);
        tf_phn1.setWidth(Display.getWidth()/2+50);
        tf_phn2.setWidth(Display.getWidth()/2+50);
        tf_msg.setWidth(Display.getWidth()/2+95);

        ta_msg = new TextArea(Display.getWidth()/2+130, 100,TextArea.FIELD_HCENTER);

         btn_add = new ImageButton(imgs_addbtn," +",ImageButton.FIELD_HCENTER);
         btn_subtract = new ImageButton(imgs_subbtn," -",ImageButton.FIELD_RIGHT);
         btn_en = new ImageButton(imgs_tmintrvl1,"Enable",ImageButton.FIELD_HCENTER);
         btn_dis = new ImageButton(imgs_tmintrvl1,"Disable",ImageButton.FIELD_HCENTER);
//       btn_time1 = new ImageButton(imgs_tmintrvl1, "15 min",ImageButton.FIELD_LEFT);
//       btn_time2 = new ImageButton(imgs_tmintrvl1, "30 min",ImageButton.FIELD_HCENTER);
//       btn_time3 = new ImageButton(imgs_tmintrvl1, "45 min",ImageButton.FIELD_RIGHT);
//       btn_time1.set_Margin(35,0, 0,0);
//       btn_time2.set_Margin(0,0, 0,0);
//       btn_time3.set_Margin(0,0, 0,0);
         //btn_add.set_Margin(0,0, 8,8);
         LabelField lf1 = new LabelField("",Display.getWidth(),45,LabelField.FIELD_LEFT);
         LabelField lf2 = new LabelField("",Display.getWidth(),45,LabelField.FIELD_LEFT);
         LabelField lf3 = new LabelField("",Display.getWidth(),45,LabelField.FIELD_LEFT);
         LabelField lf4 = new LabelField("",Display.getWidth(),45,LabelField.FIELD_LEFT);
         lf_msg = new LabelField("Alert Message",LabelField.FIELD_LEFT);
         lf_msg.set_pos(35,0);
         lf_time = new LabelField("Select Time Interval",LabelField.FIELD_LEFT);
         lf_time.set_pos(35,20);

         pbf1 = new PillButtonField("15 min");
         pbf2 = new PillButtonField("30 min");
         pbf3 = new PillButtonField("45 min");
         pbf_set = new PillButtonSet(300,40);
         pbf_set.add(pbf1);
         pbf_set.add(pbf2);
         pbf_set.add(pbf3);

        hfm1 = new H_FieldManager(tf_phn1, btn_add, true, H_FieldManager.FIELD_HCENTER);
        hfm2 = new H_FieldManager(tf_phn2, btn_subtract, true, H_FieldManager.FIELD_HCENTER);
       // hfm3 = new HorizontalFieldManager(HORIZONTAL_SCROLL_MASK);
        hfm4 = new HorizontalFieldManager(pbf1.FIELD_HCENTER);
//        hfm3.add(btn_time1);
//        hfm3.add(btn_time2);
//        hfm3.add(btn_time3);
        hfm4.add(pbf_set);
        vfm = new VerticalFieldManager();

        add(lf1);
        add(hfm1);
        add(lf_msg);
        //add(tf_msg);
        add(ta_msg);
        add(btn_en);
        add(lf_time);
        //add(hfm3);
        add(hfm4);
        System.out.println(" insert label");


        btn_add.setChangeListener(this);
        btn_subtract.setChangeListener(this);
        btn_en.setChangeListener(this);
        btn_dis.setChangeListener(this);

    }
    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        if(field == btn_add){
            insert(hfm2, 2);

        }else if(field == btn_subtract){
            delete(hfm2);
        //}
            }else if(field == btn_en){
                replace(btn_en, btn_dis);

                insert(tf_msg, 5);

            }else if(field == btn_dis){
                replace(btn_dis, btn_en);
                delete(tf_msg);
            }
    }


}

I hope your Problem will solve in this code.
于 2012-04-05T13:31:03.477 回答