我的问题是数据已经成功插入到数据库中,但是当我注册时共享偏好不起作用,之后我销毁了他们来自启动闪屏的应用程序。我的意图就像facebook应用程序,当我一次登录并再次杀死应用程序后重新打开它永远不会要求登录页面显示主页。我想要那样。任何人都告诉如何将共享偏好与数据库结合起来?
public class MainActivity extends Activity
{
EditText et,et1,et2,et3,et4,et5;
TextView tv;
Button btn;
static MyDatabase mydb;
SQLiteDatabase db;
public static final String Filename = "logindata";
public static final String key = "status";
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.submit);
et = (EditText) findViewById(R.id.first);
et1 = (EditText) findViewById(R.id.last);
et2 = (EditText) findViewById(R.id.email);
sp = getSharedPreferences(Filename,MODE_PRIVATE);
boolean res = sp.getBoolean(key,false);
if (res)
{
Intent i = new Intent(MainActivity.this,Result.class);
startActivity(i);
Toast.makeText(MainActivity.this, "sp created", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "temp", Toast.LENGTH_SHORT).show();
}
mydb = new MyDatabase(this);
db = mydb.getWritableDatabase();
ConnectivityManager connect = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = connect.getActiveNetworkInfo();
if (net!=null&&net.isConnected())
{
tv.setVisibility(View.INVISIBLE);
}
else
{
btn.setEnabled(false);
}
}
public void submitDetails(View v) {
String user = et.getText().toString();
String pwd = et1.getText().toString();
String email = et2.getText().toString();
String emailpatern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if (user.equals("")) {
et.setError("pls enter name");
} else if (pwd.equals("")) {
et1.setError("pls enter last name");
} else if (email.equals("")) {
et2.setError("Pls Enter Valid Email");
} else if (!email.matches(emailpatern)) {
et2.setError("Pls Enter Valid email charcters");
}
else if (user.equals())
{
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean(key, true);
ed.commit();
Intent i = new Intent(MainActivity.this,Result.class);
startActivity(i);
}
else {
Intent i = new Intent(MainActivity.this, Result.class);
i.putExtra("k1", user);
i.putExtra("k2", pwd);
i.putExtra("k3", email);
startActivity(i);
}
String qry = "insert into register values('"+user+"','"+pwd+"','"+email+"')";
try
{
db.execSQL(qry);
Toast.makeText(MainActivity.this, "registered", Toast.LENGTH_SHORT).show();
}catch (Exception e)
{
Log.e("TAG:InsertionException",""+e);
}
Mydatbase活动
public class MyDatabase extends SQLiteOpenHelper
{
Context c;
private static final String DBNAME = "anil";
private static final int VERSION = 1;
public MyDatabase(Context c)
{
super(c,DBNAME,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try
{
String qry = "create table register(User TEXT,Password TEXT,Email TEXT)";
db.execSQL(qry);
Toast.makeText(c,"created", Toast.LENGTH_SHORT).show();
}catch(Exception e)
{
Log.e("TAG: Table Creation",""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
logcat的
05-03 02:11:08.611 27068-27068 /? E / AndroidRuntime:致命异常:主要 java.lang.RuntimeException:无法启动活动ComponentInfo {anilkumar.com.iwtweb / anilkumar.com.iwtweb.Result}:java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException at anilkumar.com.iwtweb.Result.onCreate(Result.java:28) 在android.app.Activity.performCreate(Activity.java:4466) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) 05-03 02:11:12.162 78-111 /? E / InputDispatcher:频道'41373368 anilkumar.com.iwtweb / anilkumar.com.iwtweb.SplashActivity(server)'〜频道无法恢复,将被处置!
结果活动
public class Result extends AppCompatActivity
{
TextView tv,tv2,tv3,tv4,tv5,tv6;
String user,pwd,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
tv = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView3);
tv3 = (TextView)findViewById(R.id.textView4);
Intent i = getIntent();
Bundle b = i.getExtras();
user = b.getString("k1");
pwd =b.getString("k2");
email = b.getString("k3");
//here is error tv.setText(user);
tv2.setText(pwd);
tv3.setText(email);
}
答案 0 :(得分:1)
假设您的其他代码有效,请更改您的submitDetails
方法,如下所示:
public void submitDetails(View v) {
String user = et.getText().toString();
String pwd = et1.getText().toString();
String email = et2.getText().toString();
String emailpatern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if (user.equals("")) {
et.setError("pls enter name");
} else if (pwd.equals("")) {
et1.setError("pls enter last name");
} else if (email.equals("")) {
et2.setError("Pls Enter Valid Email");
} else if (!email.matches(emailpatern)) {
et2.setError("Pls Enter Valid email charcters");
}
else
{
//at this point you can try and insert the submitted values into the db
String qry = "insert into register values('"+user+"','"+pwd+"','"+email+"')";
try
{
db.execSQL(qry);
Toast.makeText(MainActivity.this, "registered", Toast.LENGTH_SHORT).show();
//the insert was successful - here you simply "cache" the details
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean(key, true);
ed.commit();
//then you continue to load the next Activity
Intent i = new Intent(MainActivity.this, Result.class);
i.putExtra("k1", user);
i.putExtra("k2", pwd);
i.putExtra("k3", email);
startActivity(i);
}catch (Exception e)
{
Log.e("TAG:InsertionException",""+e);
//there were some problems
}
}
}
请尝试使用这些更改,并告知我们您的问题是否已解决。