旋转手机时数据库强制关闭的微调器

时间:2011-08-15 04:48:58

标签: java android sqlite android-layout android-widget

我正在开发一个android,它包含一个由空白数据库表填充的列表视图,第一个选项为“新建”。当用户单击“新建”时,将加载用户填写的表单,并在单击表单中的“保存”时将其存储在列表视图的数据库表中。再次当用户打开此列表视图时,它将具有“新建”以及用户填写的表单作为选项说明 创建新表单和表单1.表单中很少有微调器由数据库中的另一个表填充

到此为止一切都很好。当用户单击Form1时,它会加载他已填写的表单进行编辑。

我面临的问题是每当用户加载表单并旋转手机时,它就会被强制关闭。同样的事情也发生在列表视图中。每当我进入listview并旋转手机时,它就会被强制关闭。

这是我的logcat

08-15 09:39:36.759: ERROR/AndroidRuntime(6481): FATAL EXCEPTION: main
08-15 09:39:36.759: ERROR/AndroidRuntime(6481): java.lang.NullPointerException  
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):at com.sufoin.mormeds.Submit$2.onItemSelected(Submit.java:366)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.AdapterView.selectionChanged(AdapterView.java:854)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:977)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.AdapterView.handleDataChanged(AdapterView.java:959)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:179)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481): at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.TableRow.measureChildBeforeLayout(TableRow.java:221)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.TableRow.onMeasure(TableRow.java:112)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:456)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at   android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.TableLayout.measureVertical(TableLayout.java:468)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.TableLayout.onMeasure(TableLayout.java:431)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1082)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.ScrollView.onMeasure(ScrollView.java:296)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.View.measure(View.java:8330)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1860)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.os.Looper.loop(Looper.java:123)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at android.app.ActivityThread.main(ActivityThread.java:3835)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at java.lang.reflect.Method.invoke(Method.java:507)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-15 09:39:36.759: ERROR/AndroidRuntime(6481):     at dalvik.system.NativeStart.main(Native Method)

这是发生错误的代码。它用于初始化和设置微调器

private void initandsetspinners(final Spinner gen, final Spinner prinum, final Spinner alternum, final Spinner precon, final Spinner dep, final Spinner pra, final Spinner add) {
    //Initializing the shared preferences variable
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

     //Creating an object called mydbhelper
    Dbms mydbhelper = new Dbms(this);
    //Loading the database in writable format
    final SQLiteDatabase db=mydbhelper.getWritableDatabase();

    //For Medicines Table
    try{
        //For medicines
        Cursor medspin = db.query("medicines", null,null,null,null,null,null);
        medspin.moveToPosition(prefs.getInt("pos",1));
        //Filling the variables to set selemedspintion
        deptpos=medspin.getInt(medspin.getColumnIndex("provider_department"));
        practipos=medspin.getInt(medspin.getColumnIndex("provider_practice"));
        addrpos=medspin.getInt(medspin.getColumnIndex("provider_address"));
        hsname=medspin.getString(medspin.getColumnIndex("healthsystem"));
        medspin.close();

        //For user_masterdata table
        Cursor userspin = db.query("user_masterdata", null,null,null,null,null,null);
        userspin.moveToPosition(0);
        //Filling the variables to set seleuserspintion
        genpos=userspin.getInt(userspin.getColumnIndex("gender"));
        pripos=userspin.getInt(userspin.getColumnIndex("primarynumbertype"));
        altpos=userspin.getInt(userspin.getColumnIndex("alternatenumbertype"));
        prefpos=userspin.getInt(userspin.getColumnIndex("prefcontactype"));
        userspin.close();

    }
    catch(Exception e){
        Log.e("Error","Error",e);
    }



    //Initializing the spinners
    //Gender
    ArrayAdapter<CharSequence> gentype = ArrayAdapter.createFromResource(this,R.array.gender, android.R.layout.simple_spinner_item);        
    gentype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    gen.setAdapter(gentype);

    //Primary Number
    ArrayAdapter<CharSequence> prinumtype = ArrayAdapter.createFromResource(this,R.array.pnumtype, android.R.layout.simple_spinner_item);        
    prinumtype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    prinum.setAdapter(prinumtype);

    //Alternate Number
    ArrayAdapter<CharSequence> altnumtype = ArrayAdapter.createFromResource(this,R.array.anumtype, android.R.layout.simple_spinner_item);        
    altnumtype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    alternum.setAdapter(altnumtype);

    //Preferred Contact type
    ArrayAdapter<CharSequence> preftype = ArrayAdapter.createFromResource(this,R.array.precontype, android.R.layout.simple_spinner_item);        
    preftype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    precon.setAdapter(preftype);

    try{

        //Cursor
        Cursor depcur = db.rawQuery("SELECT _id,department,"+prefs.getString("hsname",null)+ " FROM dept_masterdata WHERE "+prefs.getString("hsname",null)+"=1",null);
        depcur.moveToFirst();
        startManagingCursor(depcur);
        String[] from = new String[]{"department"};
        int[] to = new int[] { android.R.id.text1 };
         // Now creating an array adapter and set it to display using my row
        //Adaptor for Department
        SimpleCursorAdapter deptype =new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, depcur, from, to);
        deptype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dep.setAdapter(deptype);


    }
    catch(Exception e){
        Log.e("Error","Error",e);
    }
     //Now determine which department is selected
    //Department
    dep.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            deptid=dep.getSelectedItemId();

            //Setting the practice spinner based on the department selected
            Cursor pracur = db.rawQuery("SELECT _id,practice,dept_link,hs_id FROM practice_masterdata WHERE dept_link="+deptid+" AND hs_id="+(prefs.getInt("hsid",0)),null);
            pracur.moveToFirst();
            startManagingCursor(pracur);
            String[] from1 = new String[]{"practice"};
            int[] to1 = new int[]{android.R.id.text1};
            SimpleCursorAdapter practype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item, pracur, from1, to1);
            practype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            pra.setAdapter(practype);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    //Practice
    pra.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){

            practid=pra.getSelectedItemId();

            //Setting the address  based on the practice selected
            Cursor addcur =db.rawQuery("SELECT * FROM address_masterdata WHERE practice_link="+practid,null);
            addcur.moveToFirst();
            startManagingCursor(addcur);
            String[] from2 = new String[]{"address"};
            int[] to2   = new int[]{android.R.id.text1};
            SimpleCursorAdapter addtype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item,addcur, from2, to2);
            addtype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            add.setAdapter(addtype);
            practicename=pra.getSelectedItem().toString();


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    //Address
    add.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            addrid=add.getSelectedItemId();  
            addressname=add.getSelectedItem().toString();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });



    //Getting the info
    //Gender
    gen.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            gender=parent.getSelectedItem().toString();  


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    //Primary Number
    prinum.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            primarynumbertype=parent.getSelectedItem().toString();  


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    //Alternate Number 
    alternum.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            alternatenumbertype=parent.getSelectedItem().toString();  


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    //Preferred Contact type 
    precon.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
            prefferedcontactype=parent.getSelectedItem().toString();  


        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });




    //Setting the spinners based on the above variables
    gen.setSelection(genpos);
    prinum.setSelection(pripos);
    alternum.setSelection(altpos);
    precon.setSelection(prefpos);
    dep.setSelection(deptpos);
    pra.setSelection(practipos);
    add.setSelection(addrpos);



}

第366行是SimpleCursorAdapter practype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item, pracur, from1, to1);

请帮助我

此致 Chinni krishna

2 个答案:

答案 0 :(得分:1)

正如其他人所提到的,当您更改格局时,您的活动将再次创建。如果没有看到导致此问题的代码,我将无法帮助修复它。但是,如果所有其他方法都失败了,您可以在Android Manifest中的Activity上设置以下内容,以便不会在方向更改时重新创建活动:

    <activity android:name="MainActivity" android:configChanges="orientation|keyboardHidden"></activity>

答案 1 :(得分:0)

我认为这是问题所在。当您旋转手机时,您的Activty正在被销毁并重建,因此它正在丢失Cursors(我猜您正在使用Cursors来访问您的数据库)。尝试关闭Cursors中的onStop()并在onStart()中创建onDestroy()(如果这不起作用,请尝试onCreate() / onPause()onResume() / Cursor cursor = null;

修改

将其声明为您班级中的成员变量:

try {
    cursor = new Cursor(blah, blah);
}

在try块中初始化:

onDestroy

这位于if(cursor!=null) { cursor.close(); }

{{1}}