0

我有三个布局:

  1. 主布局(带有“个人资料”按钮)

  2. 配置文件布局(使用文本视图显示来自数据库的数据和两个名为“编辑”和“返回主”的按钮)

  3. 编辑布局(使用 textview 和 edittext 来编辑用户的个人资料和两个按钮,再次显示“保存”和“返回个人资料”)

按钮相关方法:(按钮加一颗星,相关方法加两星)

*个人资料:**editm(查看w)

*编辑:**调用它(查看w)

*回到主要:**backmain(查看w)

*保存: *savemethj(查看 w)

*返回个人资料:**pbackmeth(查看 w)

  public class MainActivity extends Activity
   {
    TextView t1;
EditText e1,e2;
   databasehelper helper; 
       //Profile variables
      String ptrm;
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;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     phelper=new pdatabasehelper(this);
}
     public void backmain(View w){ 
   setContentView(R.layout.activity_main);
      }
     public void editm(View w){ 
setContentView(R.layout.profilec);
          try{
                      pshow();
}
catch(Exception e){ 
    Toast.makeText(getBaseContext(), "pshow method", 5).show();
}



    }


public void pshow()
{
    onStartp();
    Log.d("7", "a");
    onstart();

    Log.d("10", "a");
    try{
        Log.d("55555", "entering to db");
       Cursor c=phelper.pshowrecord(ptrm);
       Log.d("11", "a");
       if (c!=null){

           Log.d("c1", "a");
           c.moveToFirst();

           Log.d("cmm1", "a");
        String name= c.getString(0);
           Log.d("cn1", "a");
                  kt1.setText(name);
                                  Log.d("c2", "a");
        kt2.setText(c.getString(1));
        Log.d("c3", "a");

        kt3.setText(c.getString(2));
        Log.d("12", "a");
        kt4.setText(c.getString(3));
        kt5.setText(c.getString(4));
        kt6.setText(c.getString(5));
        Log.d("13", "a");
        }
    else{
        Log.d("14", "a");
        Toast.makeText(this,"Add a Contact", 50).show(); 
    }
        }catch(Exception e){ 
      Toast.makeText(getBaseContext(), "SOme display error", 20).show();
    }
}


public void pbackmeth(View w){

    setContentView(R.layout.profilec);
    pshow();
}
public void calledit(View w)
{

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

}

public void savemethj(View w)
{ 

    onstart();
    Log.d("tag000", "before allgettext");

    long a;
    Log.d("tag01111", "afterlong");
    ptrm=ptn.getText().toString();
    try{ 
    phelper.deleterecord(ptrm);
    Log.d("tag02222", "be");
    String edt1=ed1.getText().toString();
    String edt2=ed2.getText().toString();
    Log.d("tag03333", "be11111");
    String edt3=ed3.getText().toString();
    String edt4=ed4.getText().toString();
    Log.d("tag0444444", "be2222");
    String edt5=ed5.getText().toString();
    String edt6=ed6.getText().toString();
    Log.d("tag11111", "after allgettext");

     phelper.deleterecord(ptrm);
    a=phelper.insertpro(ptrm,/*img*/edt1, edt2, edt3, edt4, edt5, edt6);
    Log.d("tag9999999", "after allgettext");

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

    }


    }
@Override
 public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

 }

数据库类:

   public class pdatabasehelper extends SQLiteOpenHelper
    {
final static String databasename="demonewexp";
final static int databaseversion=1;

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 mypro");
    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();
    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 Cursor pshowrecord(String id)
    {
     Log.d("d1", "a");
        Cursor cs = null;
        SQLiteDatabase base=getReadableDatabase();
        Log.d("d2", "a");
        try
        {
            Log.d("d3", "a");
         cs=base.query("mypro", new String[]{"name","guardian","number","address","medical","another"},"id=?", new String[]{String.valueOf(id)}, null, null, null);
         Log.d("d4", "a");
         return cs;
        }catch(Exception e){ 
            Log.d("d5", "a");
            return cs;
        }


    }


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

          }

我第一次启动我的应用程序,我点击了“配置文件”按钮,没有数据因此它显示像“一些显示错误”然后我点击编辑按钮并添加名称,数字等数据点击保存按钮,它被保存. 然后我通过单击“返回配置文件”回到配置文件布局,我可以看到我添加的配置文件或数据。然后,如果我通过单击“返回主”移回主布局,并且如果再次单击“配置文件”按钮,那么它还将带我进入配置文件布局,其中添加了每次从数据库中获取的数据。但是当我关闭我的应用程序并再次启动它时,问题就来了,我点击“配置文件”按钮,然后它没有显示任何内容,即使数据库中存在数据库值,但是当我再次启动我的应用程序时,它无法获取我之前存储的数据并显示像“某些显示错误”这样的 toast。我想检查错误并放一些“日志”然后我在使用应用程序时看到日志文件然后它显示最后一个值:

    Log.d("cmm1", "a");

之后我的应用程序停止并且日志文件中没有生成日志。这意味着我在使用时出错:

     String name= c.getString(0);

我不知道这段代码有什么问题,或者我可能有任何其他问题。请帮帮我。我错过了什么吗?

k 我已经编辑了代码以启动新的活动。我也有其他代码,但剪切粘贴代码以便于查看和查找错误。这是我现在的代码:

(第一个带有按钮配置文件的活动,单击配置文件按钮进入另一个有意图的活动)

MainActivity.class:

    public class MainActivity extends Activity {
     @Override
       protected void onCreate(Bundle savedInstanceState)
    {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);phelper=new pdatabasehelper(this);
     }

 public void editm(View w){ 

      Intent pro=new Intent(MainActivity.this,profile.class);
      startActivity(new Intent(pro));

             }
    public boolean onCreateOptionsMenu(Menu menu) 
    {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
  }

(显示个人资料)

profile.class:

    public class profile extends Activity
    {
pdatabasehelper phelper;
TextView ptn;
TextView kt0;
TextView kt1;
TextView kt2;
TextView kt3;TextView kt4;TextView kt5;TextView kt6;

      public void onCreate(Bundle savedInstanceState) 
   {
     super.onCreate(savedInstanceState);
    Toast.makeText(getBaseContext(), "Profile View", 5).show();
    setContentView(R.layout.profilec);
    Log.d("oncreate5", "5");
    ptn=(TextView)findViewById(R.id.jprofile);
    Log.d("oncreate5.666", "5.6");
    phelper=new pdatabasehelper(this);
    Log.d("oncreate5", "6");
    pshow();
    Log.d("oncreate5", "7");
    Toast.makeText(getBaseContext(), "helloo new", 10).show();

}

public void pshow(){
    Log.d("6", "a");
    //Toast.makeText(getBaseContext(), "helloo new", 10).show();
    onStartp();
    Log.d("7", "a");
    Cursor c=null;
    Log.d("8", "a");

    Log.d("9,10", "a");
    try{
        Log.d("55555", "entering to db");
    String ptrm = null;
    c=phelper.pshowrecord(ptrm);
    Log.d("11", "a");


    if(c!=null){
          Log.d("c1", "a");
           c.moveToFirst();

             Log.d("cmm1", "a");
        String name= c.getString(0);
            Log.d("cn1", "a");
            kt1.setText(name);
                Log.d("c2", "a");
        kt2.setText(c.getString(1));
        Log.d("c3", "a");

        kt3.setText(c.getString(2));
        Log.d("12", "a");
        kt4.setText(c.getString(3));
        kt5.setText(c.getString(4));
        kt6.setText(c.getString(5));
        Log.d("13", "a");
        c.close();
        Log.d("14", "a");

}
    else{

        Toast.makeText(this,"Add a Contact", 50).show(); 
    }
    }catch(Exception e){ 
        Toast.makeText(getBaseContext(), "SOme display error", 20).show();
    }
}




public void calledit(View w)
{
    Intent i=new Intent(this,editp.class);
    startActivity(i);
        //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);
}

}

(使用保存按钮添加或编辑配置文件)editp.class

     public class editp extends Activity {

EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
EditText ed5;
EditText ed6;
TextView ptn;

pdatabasehelper phelper;
public void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editc);
    phelper=new pdatabasehelper(this);

}

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


public void savemethj(View w)
{ 

    onstart();
    Log.d("tag000", "before allgettext");

    long a;
    Log.d("tag01111", "afterlong");
    String ptrm = ptn.getText().toString();
    try{ 
    phelper.deleterecord(ptrm);
    Log.d("tag02222", "be");
    String edt1=ed1.getText().toString();
    String edt2=ed2.getText().toString();
    Log.d("tag03333", "be11111");
    String edt3=ed3.getText().toString();
    String edt4=ed4.getText().toString();
    Log.d("tag0444444", "be2222");
    String edt5=ed5.getText().toString();
    String edt6=ed6.getText().toString();
    Log.d("tag11111", "after allgettext");

     phelper.deleterecord(ptrm);
    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();

    }


    }

  }

我的记录保存在数据库中,我可以看到它们,但我无法查看我第一次保存的配置文件中的那些记录,即使我调用 pshow() 从数据库中取出记录。我希望每次我来个人资料时,我都可以看到这些记录要么从编辑页面移回,要么单击主页或布局上的“个人资料”按钮。请帮我找出错误。并原谅我用来查看错误的日志命令。而且我在这里遇到错误:

  String name= c.getString(0);

因为我在日志文件中运行程序时看到的最后一个日志是:

    Log.d("cmm1", "a");

在屏幕上显示错误消息之后,像“一些显示错误”这样的吐司。

4

1 回答 1

0

您确实有很多工作要做,但请考虑一下。
代替:

public void editm(View w){ 
setContentView(R.layout.profilec);
    try{
        pshow();
    }
    catch(Exception e){ 
        Toast.makeText(getBaseContext(), "pshow method", 5).show();
    }
}

你要:

Intent edit_profile_intent = new Intent();
edit_profile_intent.setClass(getApplicationContext(), EditProfileActivity.class);
startActivity(edit_profile_intent);

所以你需要做几件事:创建这个新的活动类,在你的清单中声明它,编辑你的布局以包含适当的视图,等等。

让我们修复您的活动堆栈,然后担心您的数据问题......

于 2013-05-19T07:38:27.037 回答