1

我收到 NullPointerException:

public void llenardatos(SQLiteDatabase db, int pos) {

    ImgVector = new ImageView[50];
    String[] id = new String[] { String.valueOf(pos + 1) }; 

    EditText txttipo = (EditText) mContentView.findViewById(R.id.Txttipo);
    EditText spalto = (EditText) mContentView.findViewById(R.id.Txtalto);
    EditText splargo = (EditText) mContentView.findViewById(R.id.TxtLargo);
    EditText spancho = (EditText) mContentView.findViewById(R.id.Txtancho);
    EditText sobser = (EditText) mContentView
            .findViewById(R.id.txtobservaciones);
    EditText sphora = (EditText) mContentView.findViewById(R.id.Txthora);
    EditText txtfecha = (EditText) mContentView.findViewById(R.id.txtfecha);
    TextView txtlati = (TextView) mContentView
            .findViewById(R.id.txtlatitud);
    TextView txtlongi = (TextView) mContentView
            .findViewById(R.id.txtlongitud);

    Critico cr = new Critico();
    cr.seleccionar(db, id);
    txttipo.setText("" + cr.getTipo());
    spalto.setText("" + cr.getAlto());
    splargo.setText("" + cr.getLargo());
    spancho.setText("" + cr.getAncho());
    sobser.setText("" + cr.getObservaciones());
    sphora.setText(cr.getHora());
    txtfecha.setText("" + cr.getFecha());
    txtlati.setText("" + cr.getLatitud());
    txtlongi.setText("" + cr.getLongitud());

    FotoCritico fc = new FotoCritico();
    ArrayList<String> datos = fc.seleccionartodos(db, id);
    int i = datos.size();
    for (int contador = 0; contador < i; contador++) {
        String dato = datos.get(contador);
        Uri imgUri = Uri.parse(dato);

        LinearLayout.LayoutParams lparams4 = new LinearLayout.LayoutParams(
                500, 250, 1f);
        ImgVector[contador] = new ImageView(getActivity());
        ImgVector[contador].setLayoutParams(lparams4);
        ImgVector[contador].setImageResource(R.drawable.fotoandroid);
        ImgVector[contador].setId(contador);
        ImgVector[contador].setPadding(0, 10, 0, 10);
        lyn.addView(ImgVector[contador]);
        // personas++;
        ImgVector[contador].setImageURI(imgUri);
    }
}

我收到 NullPointerException:

04-25 08:39:57.630: E/AndroidRuntime(3358): FATAL EXCEPTION: main
04-25 08:39:57.630: E/AndroidRuntime(3358): java.lang.NullPointerException
04-25 08:39:57.630: E/AndroidRuntime(3358):     at android.view.ViewConfiguration.get(ViewConfiguration.java:276)

Eclipse 没有报告语法错误。我做错了什么?

4

1 回答 1

0

当您尝试访问值为 null 的对象时,会发生 java.lang.NullPointerException。当您尝试使用变量“lyn”或“ImgVector[contador]”时,它的值为 null。

要找出哪个变量的值为 null,请在其中放入一些 if 语句:

if (lyn == null)
  Log.d("error", "lyn has null");

if (ImgVector[contador] == null)
  Log.d("error", "ImgVector[contador] has null");

将这些行放在整个代码中,if 语句的计算结果应该为 true,这将是您需要确保变量被实例化的地方。

于 2012-04-25T14:38:08.193 回答