我有三种布局:
主要布局(带有“个人资料”按钮)
配置文件布局(使用textview显示数据库中的数据和两个名为“Edit”和“Back to Main”的按钮)
编辑布局(使用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});
}
}
我启动我的应用程序第一次,我点击了“配置文件”按钮,没有数据,因此它显示像“一些显示错误”的烤面包然后我点击编辑按钮并添加名称,数字等数据点击保存按钮,它得救了。然后我通过点击“返回个人资料”回到个人资料布局,我可以看到我添加的个人资料或数据。然后,如果我通过点击“返回主要”回到主要布局,如果我再次点击 “配置文件”按钮然后它还带我到配置文件布局,添加了每次从数据库中提取的数据。但是当我关闭我的应用程序时,它就出现了问题 并再次启动它我点击“配置文件”按钮,然后它没有显示任何数据库中的数据库值,但当我再次启动我的应用程序时,它无法获取我以前存储的数据并显示吐司像“一些显示错误”。我想查看错误并输入一些“Log”然后我看到日志文件,当我使用应用程序然后它显示最后一个值为:
Log.d("cmm1", "a");
之后我的应用程序停止了,日志文件中没有生成日志。这意味着我在工作时遇到错误:
String name= c.getString(0);
我不知道这段代码有什么问题,或者我有任何其他问题。请帮帮我。我错过了什么吗?
k我已经编辑了代码,意图启动新的Activity。我也有其他代码,但剪切粘贴代码,以便于查看和查找错误。这是我现在的代码:
(第一个带有按钮配置文件的活动,单击配置文件按钮带到另一个有意图的活动)
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是:
Log.d("cmm1", "a");
在屏幕上显示错误消息,并显示“某些显示错误”等。
答案 0 :(得分: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);
所以你需要做几件事:创建这个新的活动类,在清单中声明它,编辑你的布局以包含适当的视图等。
让我们修复您的活动堆栈,然后担心您的数据问题......