繁体   English   中英

使用用户ID和if块启动Intent

[英]Launching Intent using userID with if block

我有这个活动类,不会捕获if块。 因为我是一个初学者,所以我想不出任何其他方法来在Splash Thread之后捕获给定的userID(“ 001”)以启动特定的intent(AdminTab)。

有人可以帮我指出我做错了什么还是建议我提供一些示例代码吗? 谢谢!

公共类Splash扩展了活动{

@Override
protected void onCreate(Bundle savedInstanceState) {

    final UserFunctions userFunction = new UserFunctions();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Thread timer = new Thread(){

    public void run(){

    String id = "";
    id = userFunction.getID(getApplicationContext());

    try{
        sleep(1000);
        //Should open AdminTab if user id is "001"      
        if("001".equals(id)){

        Intent i = new Intent(getApplicationContext(), 
                    AdminTab.class);

                    startActivity(i);

        }else{

        Intent i = new Intent(getApplicationContext(), 
                    UserTab.class);

                    startActivity(i);
        }

        } catch (InterruptedException e){
        e.printStackTrace();
        }finally {


        }
    }

    };

    timer.start();
}

这是UserFunctions类的getID方法。 来自sqlite数据库处理程序的getUserID。

UserFunctions.java

public String getID(Context context) {
    String id = "";
    DatabaseHandler db = new DatabaseHandler(context);
    Cursor cursor = db.getUserID();

    if(cursor != null) {
        while(cursor.moveToNext()) {
            id = cursor.getString(0);
        }
    } else {
        id = "";
    }

    cursor.close();
    db.close();
    return id;
}

您的代码应该可以正常工作,出现错误了吗?

也许将其更改为id.equals(“ 001”)

如果这不是主要活动,那么为什么不根据需要直接发送ID?

我不确定是什么问题。

“ 001” .equals(id)中没有问题。

请检查您从getID()获取的ID。 从数据库中获取的ID出现问题。 尝试打印该值并进行交叉检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM