开发者

Spinner populated by database force closing when phone is rotated

开发者 https://www.devze.com 2023-03-28 16:03 出处:网络
I am developing an android which consists of a List View populated by a blank database table with first option as \"Create New\". When the user clicks on \"Create New\" a form is loaded which the user

I am developing an android which consists of a List View populated by a blank database table with first option as "Create New". When the user clicks on "Create New" a form is loaded which the user fills up and on clicking Save in the form this is stored in the database table of list view. Again when the user opens this list view it will have Create New as well as the form filled by user as options say Create New and Form 1. There are few spinners in the form which are populated by an another table in the database

Everything is fine till here. When the user clicks on Form1 it loads the form he has filled for editing.

The problem I am facing is whenever the user loads the form and rotates the phone it is being FORCE CLOSED. Same thing is happening with list view also. Whenever I am in listview and rotate the Phone it is getting FORCE CLOSED.

here is my 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(64开发者_Go百科81):     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)

Here is my code where the error has occured. it is used to initialize and set spinners

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);



}

Line 366 is SimpleCursorAdapter practype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item, pracur, from1, to1);

Please Kindly help me out

Regards Chinni krishna


As others have mentioned, when you change landscape, your Activity is being created again. Without seeing the code causing this, I wouldn't be able to help fix it. But if all else fails, you can set the following on your Activity in the Android Manifest to not re-create the Activity on orientation change:

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


I think this is the problem. When you rotate your phone, your Activty is being destroyed and rebuilt, so it is losing the Cursors (I'm guessing you are using Cursors to access your database). Try closing the Cursors in onStop() and creating them in onStart() (and if that doesn't work, try onDestroy()/onCreate() or onPause()/onResume()

EDIT:

Declare it as a member variable in your class:

Cursor cursor = null;

Initialise in your try block:

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

This in in onDestroy:

if(cursor!=null) {
    cursor.close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号