从数据库填充微调器时获取空指针异常

时间:2014-03-21 06:12:03

标签: android database

我现在有一个页面,其他布局已经膨胀了4次,我必须使用数据库中的值填充膨胀布局的微调器,我正在尝试但是获得空指针异常。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.drsdetail);

    changeCode = (TextView) findViewById(R.id.changeCode);
    generate = (Button) findViewById(R.id.generate);
    fromVessel = (Button) findViewById(R.id.fromVessel);
    toVessel = (Button) findViewById(R.id.toVessel);
    overTime = (EditText) findViewById(R.id.overTimeEdit);
    offShoreTime = (EditText) findViewById(R.id.offShoreEdit);
    onShoreTime = (EditText) findViewById(R.id.onShoreEdit);
    LinearLayout parent = (LinearLayout) findViewById(R.id.parentLayout);
    spinnerValues = (Spinner) findViewById(R.id.spinnerValues);
    loadSpinnerData();

    LayoutInflater inflater = (LayoutInflater)                   getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                          for(int i=0;i<4;i++){
    LinearLayout row = new LinearLayout(this);
    View layout_number = inflater.inflate(R.layout.layoutinflate, parent,
            false);

    row.addView(layout_number);
    parent.addView(row);
          }
    ArrayAdapter<String> adapterDataValues = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, dataValues);
    adapterDataValues
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerValues.setAdapter(adapterDataValues);
}

private void loadSpinnerData() {
    dataValues.clear();
    try {
        System.out.println("in try");
        DBHelper dbHelper = new DBHelper(DRSDetail.this);
        dbHelper.openDataBase();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        // project number
        Cursor crData = db.rawQuery("SELECT DRS_ID FROM  DRS_TBL", null);
        if (crData.getCount() > 0) {
            while (crData.moveToNext()) {
                dataValues.add(crData.getString(0));
                System.out.println("value of cr.getString "
                        + crData.getString(0));

            }
        }

        crData.close();
    } catch (Exception e) {
        System.out.println("exception");
    }

}

logcat详细信息:

    03-21 11:35:49.892: E/AndroidRuntime(15823): FATAL EXCEPTION: main
    03-21 11:35:49.892: E/AndroidRuntime(15823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kongsberg/com.kongsberg.ui.DRSDetail}: 
    java.lang.NullPointerException 03-21 11:35:49.892: E/AndroidRuntime(15823):     at  
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.access$600(ActivityThread.java:130)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.os.Handler.dispatchMessage(Handler.java:99)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.os.Looper.loop(Looper.java:137)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.main(ActivityThread.java:4745)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    java.lang.reflect.Method.invokeNative(Native Method)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    java.lang.reflect.Method.invoke(Method.java:511)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at                    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at dalvik.system.NativeStart.main(Native Method)
    03-21 11:35:49.892: E/AndroidRuntime(15823): Caused by: java.lang.NullPointerException
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at com.kongsberg.ui.DRSDetail.onCreate(DRSDetail.java:66)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at android.app.Activity.performCreate(Activity.java:5008)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

2 个答案:

答案 0 :(得分:3)

更改此

 spinnerValues = (Spinner) findViewById(R.id.spinnerValues);

 spinnerValues = (Spinner) layout_number.findViewById(R.id.spinnerValues);

你的Spinner属于你的膨胀布局。并且您必须在充气布局之前初始化Spinner,并且此位置为NULL

在您的视图夸大之后初始化您的Spinner

View layout_number = inflater.inflate(R.layout.layoutinflate, parent,
        false);

spinnerValues = (Spinner) layout_number.findViewById(R.id.spinnerValues);

答案 1 :(得分:1)

  

微调器来自膨胀布局

所以移动这个

spinnerValues = (Spinner) findViewById(R.id.spinnerValues);
inflate()循环之后

。在它之前,微调器尚未在您的活动视图层次结构中,并返回null。