在Android中的Activity之间移动时丢失存储的数据

时间:2013-05-17 00:42:23

标签: android android-layout android-intent

问候,请查看我的代码并帮助我找到错误。

代码:

主要活动类(Click方法上的按钮):

public void editm(View w) {
    /*this method called when i click on Profile button that is on MainActivity(A)*/
    Intent pro=new Intent(MainActivity.this,profile.class);
    startActivity(new Intent(pro));
}

意图类:

public class profile extends Activity
{
    String ptrm;
    View v=null;
    pdatabasehelper phelper;
    EditText ed1;
    EditText ed2;
    EditText ed3;
    EditText ed4;
    EditText ed5;
    EditText ed6;
    TextView ptn;
    TextView kt0;
    TextView kt1;
    TextView kt2;
    TextView kt3;
    TextView kt4;
    TextView kt5;
    TextView kt6;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profilec);
        pshow(v);
        //Toast.makeText(getBaseContext(), "helloo new", 10).show();
    }

    public void pshow(View w) {
        //this method called to fetch out the database values
        onStartp();
        onstart();
        Boolean flag=false;
        try {
            flag=phelper.pshowrecord(ptrm);
        } catch(Exception e){
            Toast.makeText(getBaseContext(), "SOme display error", 20).show();
        }
        if(flag==true) {
            kt1.setText(phelper.name);
            kt2.setText(phelper.guardian);
            kt3.setText(phelper.number);
            kt4.setText(phelper.address);
            kt5.setText(phelper.medical);
            kt6.setText(phelper.another);
        }
        else {
            Toast.makeText(this,"Add a Contact", 50).show();
        }
    }

    public void pbackmeth(View w) {
        /* this method called when i move back from edit activity(C)
         * to profile activity(B)
         */
        setContentView(R.layout.profilec);
        pshow(v);
    }

    public void calledit(View w)
    {
        // this method called when i click on edit button,which is
        // there below the profile.
        setContentView(R.layout.editc);
    }

    public void onStartp() {
        kt1=(TextView)findViewById(R.id.ptext3);
        kt2=(TextView)findViewById(R.id.ptext5);
        kt3=(TextView)findViewById(R.id.ptext7);
        kt4=(TextView)findViewById(R.id.ptext9);
        kt5=(TextView)findViewById(R.id.ptext11);
        kt6=(TextView)findViewById(R.id.ptext13);
    }

    public void onstart() {

        ed1=(EditText)findViewById(R.id.jname);
        ed2=(EditText)findViewById(R.id.jeditText1);
        ed3=(EditText)findViewById(R.id.jeditText2);
        ed4=(EditText)findViewById(R.id.jeditText3);
        ed5=(EditText)findViewById(R.id.jeditText4);
        ed6=(EditText)findViewById(R.id.jaeditText1);
        ptn=(TextView)findViewById(R.id.jprofile);
        phelper=new pdatabasehelper(this);
    }

    public void savemethj(View w)
    {
        // this method called when i click on Save button which is
        // in activity(C).
        onstart();

        long a;

        ptrm=ptn.getText().toString();

        String edt1=ed1.getText().toString();
        String edt2=ed2.getText().toString();

        String edt3=ed3.getText().toString();
        String edt4=ed4.getText().toString();

        String edt5=ed5.getText().toString();
        String edt6=ed6.getText().toString();

        try {
            a=phelper.insertpro(ptrm,edt1, edt2, edt3, edt4, edt5, edt6);

            if(a>=1) {
                Toast.makeText(getBaseContext(),a+ "Record Successfully Saved", 30).show();
            }
            else {
                Toast.makeText(getBaseContext(),  "Not Saved", 30).show();
            }
        }catch(Exception e) {
            Toast.makeText(getBaseContext(), "Errrrrrrrrrrr", Toast.LENGTH_SHORT).show();
        }
    }
}

数据库类:

public class pdatabasehelper extends SQLiteOpenHelper {

    final static String databasename="demop";
    final static int databaseversion=1;
    String name,guardian,number,address,medical,another=null;

    public pdatabasehelper(Context ctx) {
        super(ctx,databasename,null,databaseversion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            Log.d("tag4545","database");
            db.execSQL("create table mypro(id text,name text,guardian text,number text,address text,medical text,another text)");
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if Exists mypic");
        //  db.execSQL("drop table if Exists emer");
        onCreate(db);
    }

    public long insertpro(String id,String e1,String e2,String e3,String e4,String e5,String e6)
    {
        SQLiteDatabase base=getWritableDatabase();
        ContentValues value=new ContentValues();
        byte[] data = getBitmapAsByteArray(img);
        value.put("id", id);
        value.put("name", e1);
        value.put("guardian", e2);
        value.put("number", e3);
        value.put("address", e4);
        value.put("medical", e5);
        value.put("another", e6);

        long a=base.insert("mypro",null,value);

        return a;
    }

    public boolean pshowrecord(String id)
    {
        SQLiteDatabase base=getWritableDatabase();
        try
        {
            Cursor cs=base.query("mypro", new String[]{"name","guardian","number","address","medical","another"},"id=?", new String[]{String.valueOf(id)}, null, null, null);

            if(cs!=null) {

                cs.moveToFirst();
                 Log.d("Big Error"," Programe Stop");
                name=cs.getString(0);
                guardian=cs.getString(1);
                number=cs.getString(2);
                address=cs.getString(3);
                medical=cs.getString(4);
                another=cs.getString(5);
                return true;
            }
            else {
                return false;
            }
        }
        catch(Exception e)
        {
            return false;
        }
    }

    public void deleterecord(String pe_id)
    {
        SQLiteDatabase base=getWritableDatabase();
        base.delete("mypro","id=?",new String[]{pe_id});
    }
}

我希望每次通过MainActivity Button(Profile)或Activity C按钮(返回配置文件)调用我的意图(活动B或配置文件),然后我从数据库中获取所有值以在活动B或配置文件上显示它。

我收到错误,我打印“Log.d(”Big Error“,”Programe Stop“);”在pshowrecord(String id)中。但是,当我在编辑配置文件中输入值时,即当我编辑配置文件值并移回配置文件时,此方法可以正常工作。但当我再次启动我的应用程序并在第一个活动即主活动中单击配置文件按钮时调用它,然后生成此日志并且即使我在数据库中具有值,因此应用程序停止工作,因为当我单击按钮时显示返回个人资料“。

请帮忙,一天后我需要这段代码。谢谢

Xml代码:

MainActivity xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
     android:layout_height="match_parent"
    tools:context=".MainActivity" 
   android:background="@drawable/gb28"
  >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="edit"
        android:onClick="editm" />


  </RelativeLayout>

个人资料xml代码:

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gb2"
     >

    <TextView
        android:id="@+id/ptext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="PROFILE"
        android:textAppearance="?android:attr/textAppearanceLargeInverse" />

    <TextView
        android:id="@+id/ptext2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ptext1"
        android:layout_marginTop="16dp"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLargeInverse" />

    <TextView 
        android:id="@+id/ptext3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/ptext2"
          android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"

        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />

            <TextView 
        android:id="@+id/ptext4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="guardian"
        android:layout_below="@+id/ptext3"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                    <TextView 
        android:id="@+id/ptext5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"
        android:layout_below="@+id/ptext4"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                            <TextView 
        android:id="@+id/ptext6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="Cell Number"
        android:layout_below="@+id/ptext5"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                                    <TextView 
        android:id="@+id/ptext7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"
        android:layout_below="@+id/ptext6"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
        <TextView 
        android:id="@+id/ptext8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="Address"
        android:layout_below="@+id/ptext7"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                <TextView 
        android:id="@+id/ptext9"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:layout_below="@+id/ptext8"
       android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"

        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                        <TextView 
        android:id="@+id/ptext10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="Medical Problem"
        android:layout_below="@+id/ptext9"


        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        />
                        <TextView
                            android:id="@+id/ptext11"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                           android:layout_below="@id/ptext10"
                             android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"
                            android:textAppearance="?android:attr/textAppearanceLargeInverse" />

                        <TextView
                            android:id="@+id/ptext12"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                           android:layout_below="@id/ptext11"
                           android:text="Another Description"
                            android:textAppearance="?android:attr/textAppearanceLargeInverse" />


                        <TextView
                            android:id="@+id/ptext13"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                           android:layout_below="@id/ptext12"
                             android:inputType="textMultiLine"
                           android:isScrollContainer="true"
                           android:minHeight="20dp"
              android:textAppearance="?android:attr/textAppearanceLargeInverse" />
                        <Button 
                            android:id="@+id/peditb"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:layout_below="@id/ptext13"
                            android:text="Edit Profile"
                            android:onClick="calledit"
                            />
        </RelativeLayout>

  </ScrollView>

编辑活动xml代码:

 <?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
 android:layout_height="match_parent" 

  android:background="@drawable/gb2"
 >
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/gb2"
    >

    <TextView
        android:id="@+id/jprofile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="PROFILE"
        android:textAppearance="?android:attr/textAppearanceLargeInverse" />

     <TextView
         android:id="@+id/jtextView5"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/jeditText2"
         android:layout_below="@+id/jeditText2"
         android:text="Address"
         android:textAppearance="?android:attr/textAppearanceLargeInverse" />

     <EditText
         android:id="@+id/jeditText3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/jtextView5"
         android:layout_alignParentRight="true"
         android:layout_below="@+id/jtextView5"
         android:ems="10"
         android:hint="Home_Address"
         android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp"
          />

     <TextView
         android:id="@+id/jtextView6"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/jeditText3"
         android:text="Medical Problem"
         android:textAppearance="?android:attr/textAppearanceLargeInverse" />

     <EditText
         android:id="@+id/jeditText4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_below="@+id/jtextView6"
         android:ems="10"
         android:hint="Medical Problems or Alergies"
         android:inputType="textMultiLine"
    android:isScrollContainer="true"
    android:minHeight="20dp"
          />

     <TextView
         android:id="@+id/jtextView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/jtextView6"
         android:layout_below="@+id/jtextView1"
         android:layout_marginTop="28dp"
         android:text="Name"
         android:textAppearance="?android:attr/textAppearanceLargeInverse" />


     <TextView
         android:id="@+id/jtextView4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/jtextView3"
         android:layout_below="@+id/jeditText1"
         android:text="Cell Number"
         android:textAppearance="?android:attr/textAppearanceLargeInverse" />

     <EditText
         android:id="@+id/jeditText2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignRight="@+id/jname"
         android:layout_below="@+id/jtextView4"
         android:ems="10"
         android:hint="Number"
         android:inputType="textMultiLine"
         android:isScrollContainer="true"
         android:minHeight="20dp" />

      <Button
          android:id="@+id/jphoto"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/jeditText2"
          android:layout_alignBottom="@+id/jeditText2"
          android:layout_toRightOf="@+id/jprofile"
          android:text="Take Photo" 
          android:onClick="cammethod"
          />

      <EditText
          android:id="@+id/jname"
          android:layout_width="170dp"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignRight="@+id/jtextView6"
          android:layout_below="@+id/jtextView2"
          android:ems="10"
          android:hint="Enter_Name"
          android:inputType="textMultiLine"
          android:isScrollContainer="true"
          android:minHeight="20dp" >

          <requestFocus />
      </EditText>

      <TextView
          android:id="@+id/jtextView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/jname"
          android:text="Guardian"
          android:textAppearance="?android:attr/textAppearanceLargeInverse" />

      <EditText
          android:id="@+id/jeditText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignRight="@+id/jeditText2"
          android:layout_below="@+id/jtextView3"
          android:ems="10"
          android:hint="Guardian Name"
          android:inputType="textMultiLine"
          android:isScrollContainer="true"
          android:minHeight="20dp" />

      <TextView
          android:id="@+id/jatextView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/jeditText4"
          android:text="Another Description"
          android:textAppearance="?android:attr/textAppearanceLargeInverse" />

      <EditText
          android:id="@+id/jaeditText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/jatextView1"
          android:ems="10"
          android:hint="Another Description"
          android:inputType="textMultiLine"
          android:isScrollContainer="true"
          android:minHeight="20dp" />

      <Button
          android:id="@+id/jsaveb"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignRight="@+id/jtextView5"
          android:layout_below="@+id/jaeditText1"
          android:onClick="savemethj"
          android:text="Save" />

      <Button 
           android:id="@+id/jback"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@+id/jsaveb"
          android:layout_below="@+id/jaeditText1"
          android:onClick="pbackmeth"
          android:text="Back to Profile" />
          />

  </RelativeLayout>

 </ScrollView>  

1 个答案:

答案 0 :(得分:1)

在导航活动堆栈时,您应该考虑使用CursorLoader让您在查看数据时保持新鲜感。

http://developer.android.com/reference/android/content/CursorLoader.html