遇到问题跳绳活动 [英] Having trouble skipping an activity

查看:122
本文介绍了遇到问题跳绳活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序有一个登录界面,并在他的仪表盘的用户登录。但我不希望用户登录每次关闭应用程序并启动它(除非他们从仪表板退出)的时间。所以,我创建了一个类来检查,如果用户登录

CheckLoggedIn.java

 公共类CheckLoggedIn延伸活动{

    私人布尔isLoggedIn = FALSE;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);

        如果(!isLoggedIn){
            意向意图=新的意图(这一点,MainActivity.class);
            startActivity(意向);
        }其他{
            意向意图=新的意图(这一点,AgentHome.class);
            startActivity(意向);
        }

    }

    公共布尔的getStatus(){
        返回this.isLoggedIn;
    }

    公共无效setStatus(布尔状态){
        isLoggedIn =状态;
    }

}
 

当用户登录时,我改变布尔 isLoggedIn 时注销。但是,当我登录成功,并关闭它,然后重新启动它检​​查了它,它还是到LoginActivity。为什么呢?

编辑:现在,我已经知道这是因为我在年初设定的布尔 isLoggedIn 该CheckLoggedIn.java。我怎样才能做到我想要做什么?

编辑2:

preference.java

 公共类preference {

    上下文语境;
    共享preferences共享preF;

    公共preference(上下文的背景下){
        this.context =背景;
        共享preF = context.getShared preferences(LoginState,0);
    }

    公共布尔getIsLoggedIn(){
        返回共享pref.getBoolean(国家,假);
    }

    公共无效setIsLoggedIn(布尔州){
        共享preferences.Editor编辑=共享pref.edit();
        editor.putBoolean(国家,状态);
        editor.commit();
    }

}
 

MainActivity.java

 公共类MainActivity延伸活动{
    preference preference =新的preference(本);
    @覆盖
    保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    如果(preference.getIsLoggedIn()){
        Log.d(国家,已登录);
    }
            ......
    }
}
 

的logcat

  02-17 16:30:53.063:E / AndroidRuntime(21450):致命异常:主要
02-17 16:30:53.063:E / AndroidRuntime(21450):工艺:collector.lbfinance,PID:21450
02-17 16:30:53.063:E / AndroidRuntime(21450):java.lang.RuntimeException的:无法实例活动ComponentInfo {collector.lbfinance / collector.lbfinance.MainActivity}:显示java.lang.NullPointerException
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.os.Handler.dispatchMessage(Handler.java:102)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.os.Looper.loop(Looper.java:136)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread.main(ActivityThread.java:5017)
02-17 16:30:53.063:E / AndroidRuntime(21450):在java.lang.reflect.Method.invokeNative(本机方法)
02-17 16:30:53.063:E / AndroidRuntime(21450):在java.lang.reflect.Method.invoke(Method.java:515)
02-17 16:30:53.063:E / AndroidRuntime(21450):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 16:30:53.063:E / AndroidRuntime(21450):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 16:30:53.063:E / AndroidRuntime(21450):在dalvik.system.NativeStart.main(本机方法)
02-17 16:30:53.063:E / AndroidRuntime(21450):由:显示java.lang.NullPointerException
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.content.ContextWrapper.getShared preferences(ContextWrapper.java:173)
02-17 16:30:53.063:E / AndroidRuntime(21450):在collector.lbfinance.library preference< INIT>(preference.java:13)
。02-17 16:30:53.063:E / AndroidRuntime(21450):在collector.lbfinance.MainActivity< INIT>(MainActivity.java:52)
02-17 16:30:53.063:E / AndroidRuntime(21450):在java.lang.Class.newInstanceImpl(本机方法)
02-17 16:30:53.063:E / AndroidRuntime(21450):在java.lang.Class.newInstance(Class.java:1208)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-17 16:30:53.063:E / AndroidRuntime(21450):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-17 16:30:53.063:E / AndroidRuntime(21450):11 ...更多
 

解决方案

您将不得不使用<一个href="https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCkQFjAA&url=http://developer.android.com/reference/android/content/Shared$p$pferences.html&ei=jOMBU7feKceJrQeXbA&usg=AFQjCNFyaMlF7pqBKbWPbXr7H1Wg4gKaoA&sig2=yrJ2GvdtrrSH4Lel60fqNw&bvm=bv.61535280,d.bmk"相对=nofollow>共享preferences 的用于这一目的。你的布尔变量是唯一有效的,直到你的活动是不是destroyed.To持久化数据,你将不得不使用共享preferences或<一href="https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&sqi=2&ved=0CEUQFjAB&url=http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html&ei=OeQBU9z4LsTArAeCqIH4AQ&usg=AFQjCNGHrlZI8DewY8QwqR2vRa37N2GJpg&sig2=z3vLWg13ITiAFPqIT1R2gw&bvm=bv.61535280,d.bmk"相对=nofollow> sqlite的。

使用SQLite只是建议,当我们有关系型数据库,对应用程序seetings等共享preferences轻量级持久存储是最好的。

 公共类preferenceForApp {
  上下文语境;
  共享preferences preFS;

  公共preferenceForApp(上下文的背景下){
      this.context =背景;
      preFS = context.getShared preferences(对myApp preFS,0);
  }

  公布尔getIsDeviceValidated(){

      返回prefs.getBoolean(验证,假);
  }

  公共无效setIsDeviceValidated(布尔值){

      共享preferences.Editor编辑器= prefs.edit();
      editor.putBoolean(验证,值);
      editor.commit();
  }
}
 

修改

在你的Activity调用这个函数,并通过上下文它以下列方式:

  preferenceForApp我的preFS =新的preferenceForApp(本);

布尔VAL =我的prefs.getIsDeviceValidated();
 

My Android app has a login screen and the user logs in to his dashboard. But I don't want the user to login every time they close the app and start it (unless they logout from the dashboard). So, I created a class to check if the user logged in

CheckLoggedIn.java

public class CheckLoggedIn extends Activity {

    private boolean isLoggedIn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        if(!isLoggedIn){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }else{
            Intent intent = new Intent(this, AgentHome.class);
            startActivity(intent);
        }

    }

    public boolean getStatus(){
        return this.isLoggedIn;
    }

    public void setStatus(boolean status){
        isLoggedIn = status;
    }

}

When user logs in, I change the boolean isLoggedIn to true and false when logs out. But when I checked it by logging in successfully and closing it and starting it again, it still goes to LoginActivity. Why?

Edit : Now, I've understood it's because I set the boolean isLoggedIn to false at the beginning of the CheckLoggedIn.java. How can I achieve what I'm trying to do?

EDIT 2 :

Preference.java

public class Preference {

    Context context;
    SharedPreferences sharedPref;

    public Preference(Context context){
        this.context = context;
        sharedPref = context.getSharedPreferences("LoginState", 0);
    }

    public boolean getIsLoggedIn(){
        return sharedPref.getBoolean("State", false);
    }

    public void setIsLoggedIn(boolean state){
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("State", state);
        editor.commit();
    }

}

MainActivity.java

public class MainActivity extends Activity {
    Preference preference = new Preference(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(preference.getIsLoggedIn()){
        Log.d("State", "Already logged in");
    }
            ......
    }
}

Logcat

02-17 16:30:53.063: E/AndroidRuntime(21450): FATAL EXCEPTION: main
02-17 16:30:53.063: E/AndroidRuntime(21450): Process: collector.lbfinance, PID: 21450
02-17 16:30:53.063: E/AndroidRuntime(21450): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{collector.lbfinance/collector.lbfinance.MainActivity}: java.lang.NullPointerException
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.os.Looper.loop(Looper.java:136)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at java.lang.reflect.Method.invoke(Method.java:515)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at dalvik.system.NativeStart.main(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450): Caused by: java.lang.NullPointerException
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at collector.lbfinance.library.Preference.<init>(Preference.java:13)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at collector.lbfinance.MainActivity.<init>(MainActivity.java:52)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at java.lang.Class.newInstanceImpl(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at java.lang.Class.newInstance(Class.java:1208)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-17 16:30:53.063: E/AndroidRuntime(21450):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-17 16:30:53.063: E/AndroidRuntime(21450):    ... 11 more

解决方案

You will have to use shared preferences for this purpose. Your Boolean variable is only valid till your activity is not destroyed.To persist data you will have to use shared preferences or sqlite.

Using sqlite is only advised when we have Relational database ,for lightweight persistent storage of app seetings etc shared preferences is best .

public class PreferenceForApp {
  Context context;
  SharedPreferences prefs;

  public PreferenceForApp(Context context) {
      this.context = context;
      prefs = context.getSharedPreferences("myAppPrefs", 0);
  }

  public Boolean getIsDeviceValidated() {

      return prefs.getBoolean("Validated", false);
  }

  public void setIsDeviceValidated(Boolean value) {

      SharedPreferences.Editor editor = prefs.edit();
      editor.putBoolean("Validated", value);
      editor.commit();
  }
}

EDIT

In your Activity call this function and pass context to it in the following way :

PreferenceForApp myPrefs= new PreferenceForApp (this);

Boolean val=myPrefs.getIsDeviceValidated();

这篇关于遇到问题跳绳活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆