Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Help me how save a state of Activity?
I explain my problem:
I Have many Activities in my Program
I Send a Data from One Activity to "Dr_see_Patient_Pro" Activity by the following code:

Intent i=new Intent(this,Dr_see_Patient_Pro.class);
   i.putExtra("idPFf2",id);
   startActivity(i);


So I retrieve this data in "Dr_see_Patient_Pro" Activity in this way:
idPatient= getIntent().getExtras().getString("idPFf2");
   int id=Integer.parseInt(idPatient);


And I don't have any Problem in there
AND After
I Want to Send "idPatient" In "Dr_see_Patient_Pro" Activity To Another Activity which is (write_history) Activity by the following Code:

Intent intent=new Intent(this,write_History.class);
    intent.putExtra("idPFf3",idPatient);
    startActivity(intent);


And i have no Problem to Retrieve the "idPatient" in "write_history" Activity
BUT
When I Want to Back From "write_history" Activity to "Dr_see_Patient_Pro" Activity I have a Problem And this Error:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Dr_see_Patient_Pro}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access$600(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
    at com.example.myapplication.Dr_see_Patient_Pro.onCreate(Dr_see_Patient_Pro.java:37)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)




and the problem is in line 37 and its a code of line 37:
System.out.println(idPatient)


What I have tried:

And its the code of My Activities: 1/ Dr_see_Patient_Pro

public class Dr_see_Patient_Pro extends AppCompatActivity {

TextView tx, tx1, tx2, tx3, tx4, tx5, tx6;
public static String getInList;
MyDB db;

public  String idPatient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dr_see__patient__pro);

    db = new MyDB(this);

    tx=(TextView)findViewById(R.id.dr_nameP);
    tx1=(TextView)findViewById(R.id.dr_ageP);
    tx2=(TextView)findViewById(R.id.dr_genderP);
    tx3=(TextView)findViewById(R.id.dr_cityP);
    tx4=(TextView)findViewById(R.id.dr_phoneP);




     idPatient= getIntent().getExtras().getString("idPFf2");
    System.out.println(idPatient);

    int id=Integer.parseInt(idPatient);


    ///phone

    Cursor dataname = db.getPaientInfo(id);

    while (dataname.moveToNext()) {
        String name1 = dataname.getString(0);
        String name2 = dataname.getString(1);
        tx.setText(name1+" "+name2);
    }



    Cursor dataPhone = db.getPaientInfo(id);
    while (dataPhone.moveToNext()) {
        String name2 = dataPhone.getString(2);
        tx4.setText(name2);
    }


    Cursor dataAge = db.getPaientInfo(id);
    while (dataAge.moveToNext()) {
        String name2 = dataAge.getString(3);
        tx1.setText(name2);
    }


    Cursor dataGender = db.getPaientInfo(id);
    while (dataGender.moveToNext()) {
        String name2 = dataGender.getString(4);
        tx2.setText(name2);
    }


    Cursor dataCity = db.getPaientInfo(id);
    while (dataCity.moveToNext()) {
        String name2 = dataCity.getString(5);
        tx3.setText(name2);
    }







}


public void writeHistory(View v){
    Intent intent=new Intent(this,write_History.class);
    intent.putExtra("idPFf3",idPatient);
    startActivity(intent);
}



public void SeeHistory(View view){
    Intent intent=new Intent(this,see_history.class);
    startActivity(intent);
}






2/write_history

public class write_History extends AppCompatActivity {

    MyDB myDB;
    String raport,charasar;
    AlertDialog.Builder builder;

    TextInputLayout edt;
    TextInputEditText edt1;

    EditText edt2;

    public String drID,DrIdS;
    public  int idDr;
public  int id;
public  String idPatient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write__history);

        myDB = new MyDB(this);

        builder = new AlertDialog.Builder(this);

        edt=findViewById(R.id.edt1);
        edt1=findViewById(R.id.edtText1);


        edt2=findViewById(R.id.edt2);


         idPatient= getIntent().getExtras().getString("idPFf3");
        System.out.println(idPatient);
         id=Integer.parseInt(idPatient);



        drID= loginPage.Dr();
        Cursor data = myDB.getIDdr(drID);
        while (data.moveToNext()) {
            DrIdS = data.getString(0);
            idDr=Integer.parseInt(DrIdS);
        }


        System.out.println("dr id " + idDr+"   p "+id);




    }



    private boolean validat() {
        String emailInput = edt1.getText().toString();

        if (emailInput.isEmpty()) {
            edt.setError("Field can't be empty");
            return false;
        }

        else {
            edt.setError(null);
            return true;
        }
    }



    public void saveHistory(View view){
        if( !validat()) {
            return;
        }
else {
            DialogMassage();

        }

    }





    public void DialogMassage() {
        myDB = new MyDB(this);

        raport=edt1.getText().toString();
        charasar=edt2.getText().toString();

        setTitle("ئاگاداربە!");
        builder.setMessage("ئایا دڵنیای لە پاشەکەوت کردنی زانیاریەکان؟")
                .setCancelable(false) .setPositiveButton("بەڵێ", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        Boolean result=myDB.SaveHistory(raport,charasar,idDr,id);
                        System.out.println(idDr+" "+id);
                        changeA();


                    }
                })
.setNegativeButton("نەخێر", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
        }
        });

        AlertDialog alert = builder.create();
        alert.show();

    }


    public void changeA(){

        Intent i=new Intent(this,Dr_see_Patient_Pro.class);
        i.putExtra("idPFf2",id);
        startActivity(i);

    }


}
Posted
Comments
David Crow 12-Mar-20 11:49am    
You do realize this is the Quick Answers forum, don't you? For detailed questions and answers, consider using the actual Android forum.

Have you considered checking to see if the key actually exists before trying to retrieve it, like:

if (getIntent().getExtras().containsKey("idPFf2"))
{
    idPatient = getIntent().getExtras().getString("idPFf2");
    System.out.println(idPatient);
    int id = Integer.parseInt(idPatient);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900