【问题标题】:Android whrere am i wrong ? uncaught exception (group=0x4001d800)Android 我错了吗?未捕获的异常(组=0x4001d800)
【发布时间】:2012-05-24 19:49:50
【问题描述】:

这项工作基于 Patel Deval 示例。

应用程序打算根据选定的单选按钮加载 ImageSwicher 内容。在运行时打印未捕获的异常 (group=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

【问题讨论】:

  • 请修正您的问题的格式。
  • 谢谢老兄,我在重新格式化代码时发现了错误。

标签: java android radio-button nullpointerexception


【解决方案1】:

isSwitcher = setFactory(this);

->

setFactory 返回 null

->

如果(标志) isSwitcher.setImageResource(pics[位置]); 别的 isSwitcher.setImageResource(其他[位置]);

在这里抛出 NPE,因为 isSwitcher 为空

【讨论】:

  • 如何从按钮事件中访问选定的图像。你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多