这项工作基于Patel Deval示例。

该应用程序打算基于选定的单选按钮加载ImageSwicher Content。在运行期间,将打印未捕获的异常(组= 0x4001d800)。我哪里错了?这是代码

private Gallery gallery;

private RadioGroup rdgSelection;

private ImageSwitcher isSwitcher;

private Boolean flag = true;

private int radioCheckedId = -1; //no radio button

//arrays of image for alerts

private Integer[] pics ={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,R.drawable.e,R.drawable.exit, R.drawable.plan, R.drawable.icon, R.drawable.plan };

private Integer[] others = {R.drawable.exit, R.drawable.plan, R.drawable.icon, R.drawable.plan,R.drawable.c };



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sendalert);

    gallery = (Gallery)findViewById(R.id.imgGallery);

    isSwitcher = (ImageSwitcher)findViewById(R.id.ImgSwith);

    rdgSelection = (RadioGroup)findViewById(R.id.radEvent);


    isSwitcher = setFactory(this);

    //get radio button id

    rdgSelection.check(rdgSelection.getChildAt(rdgSelection.getChildCount() - 1).getId());

    if (rdgSelection.getCheckedRadioButtonId()==R.id.radBtnAccident){

        flag = true;


                    gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

    }else{

        flag=false;

        gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

    }

     rdgSelection.setOnCheckedChangeListener(new OnCheckedChangeListener() {


            public void onCheckedChanged(RadioGroup group, int checkedId) {

                gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

                if (rdgSelection.getCheckedRadioButtonId() == R.id.radBtnAccident)

                    flag = true;

                else

                    flag = false;
            }
        });

        gallery.setOnItemSelectedListener(this);

}


private ImageSwitcher setFactory(MsgToWeb msgToWeb) {
    // TODO Auto-generated method stub
    return null;
}


public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;

    }

    public Object getItemId() {
        // TODO Auto-generated method stub
        return null;
    }

    public int getCount() {
        //return num of arrays
        if (flag)
            return pics.length;
        else
            return others.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext);

        if(flag)
            i.setImageResource(pics[position]);
        else
            i.setImageResource(others[position]);

        return i;
    }

}

public View makeView() {
    // TODO Auto-generated method stub

    ImageView iView = new ImageView(this);

    iView.setBackgroundColor(0xFF000000);

    iView.setScaleType(ImageView.ScaleType.FIT_CENTER);

    iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    return iView;

}


public void onItemSelected(AdapterView<?> arg0, View v, int position,long id) {
    // TODO Auto-generated method stub


    if(flag)
        isSwitcher.setImageResource(pics[position]);
    else
        isSwitcher.setImageResource(others[position]);


}


public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}


}


这里的日志

05-24 10:07:03.394: W/dalvikvm(321): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-24 10:07:03.404: E/AndroidRuntime(321): FATAL EXCEPTION: main
05-24 10:07:03.404: E/AndroidRuntime(321): java.lang.NullPointerException
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.moLaroute.mu.MsgToWeb.onItemSelected(MsgToWeb.java:147)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView.access$200(AdapterView.java:42)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Handler.handleCallback(Handler.java:587)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-24 10:07:03.404: E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:07:03.404: E/AndroidRuntime(321):  at java.lang.reflect.Method.invoke(Method.java:521)
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-24 10:07:03.404: E/AndroidRuntime(321):  at dalvik.system.NativeStart.main(Native Method)
05-24 10:07:07.014: I/Process(321): Sending signal. PID: 321 SIG: 9

最佳答案

isSwitcher = setFactory(this);

->

setFactory返回null

->

如果(标志)
isSwitcher.setImageResource(pics [position]);
其他
isSwitcher.setImageResource(others [position]);

因为isSwitcher为null,所以在此处抛出NPE

10-06 08:48