开发者社区> 问答> 正文

关于一个Activity跳转到另外一个Activity失败的问题

爵霸 2016-03-05 13:42:51 2023
package com.example.t;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText mAccount;
private EditText mPwd;
private Button mLoginButton;
private View loginView;
private View loginSuccessView;
private TextView loginSuccessShow;
private LoginDataManager mUserDataManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginpage);

    mAccount=(EditText) findViewById(R.id.login_edit_account);
    mPwd = (EditText) findViewById(R.id.login_edit_pwd);
    mLoginButton = (Button) findViewById(R.id.login_btn_login);
    loginView=findViewById(R.id.login_view);
    loginSuccessView=findViewById(R.id.login_success_view);
    loginSuccessShow=(TextView) findViewById(R.id.login_success_show);

    mLoginButton.setOnClickListener(mListener);

    if (mUserDataManager == null) {
        mUserDataManager = new LoginDataManager(this);
        mUserDataManager.openDataBase();
    }

}

OnClickListener mListener = new OnClickListener() {
    public void onClick(View v) {
        int id=v.getId();
        if( id==R.id.login_btn_login){
            login();

        }
        }

};

public void login() {
    if (isUserNameAndPwdValid()) {
        String userName = mAccount.getText().toString().trim();
        String userPwd = mPwd.getText().toString().trim();
        int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
        if(result==1){
            //login success
            loginView.setVisibility(View.GONE);
            loginSuccessView.setVisibility(View.VISIBLE);
            loginSuccessShow.setText(getString(R.string.user_login_sucess, userName));
            Toast.makeText(this, getString(R.string.login_sucess),
                    Toast.LENGTH_SHORT).show();
        }else if(result==0){
            //login failed,user does't exist
            Toast.makeText(this, getString(R.string.login_fail),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

public void register() {
    if (isUserNameAndPwdValid()) {
        String userName = mAccount.getText().toString().trim();
        String userPwd = mPwd.getText().toString().trim();
        //check if user name is already exist
        int count=mUserDataManager.findUserByName(userName);

        if(count>0){
            Toast.makeText(this, getString(R.string.name_already_exist, userName),
                    Toast.LENGTH_SHORT).show();
            return ;
        }

    }
}

public boolean isUserNameAndPwdValid() {
    if (mAccount.getText().toString().trim().equals("")) {
        Toast.makeText(this, getString(R.string.account_empty),
                Toast.LENGTH_SHORT).show();
        return false;
    } else if (mPwd.getText().toString().trim().equals("")) {
        Toast.makeText(this, getString(R.string.pwd_empty),
                Toast.LENGTH_SHORT).show();
        return false;
    }
    return true;
}

@Override
protected void onResume() {
    if (mUserDataManager == null) {
        mUserDataManager = new LoginDataManager(this);
        mUserDataManager.openDataBase();
    }
    super.onResume();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onPause() {
    if (mUserDataManager != null) {
        mUserDataManager.closeDataBase();
        mUserDataManager = null;
    }
    super.onPause();
}
}
另一个
package com.example.t;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
/**
@author Administrator
@description 首页
*/
public class HomePageActivity extends Activity implements OnItemClickListener {
private boolean[] isPermitted;
int drawableitems[] = { R.drawable.aa,R.drawable.bb,R.drawable.cc,R.drawable.dd,R.drawable.ee,R.drawable.ff,R.drawable.gg};
String[] itemsnames = { "最热单品","时尚饰品","珠宝首饰", "品质手表","潮流眼镜","绅士配件","DIY" };
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
GridView gridview = (GridView) findViewById(R.id.GridView);
List> itemList = new ArrayList>();
for (int i = 0; i < itemsnames.length; i++) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("ItemImage", drawableitems[i]);
    map.put("ItemText", itemsnames[i]);
    itemList.add(map);
}

SimpleAdapter saMenuItem = new SimpleAdapter(this, itemList, // 数据源
        R.layout.homeitems, // xml实现
        new String[] { "ItemImage", "ItemText" }, // 对应map的Key
        new int[] { R.id.ItemImage, R.id.ItemText }); // 对应R的Id

// 添加Item到网格中
gridview.setAdapter(saMenuItem);
gridview.setOnItemClickListener(this);

//permit setting
isPermitted = new boolean[saMenuItem.getCount()];
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
System.out.println("id:" + id);
System.out.println("position:" + position);
// String[] itemsnames = { "最热单品","时尚饰品","珠宝首饰", "品质手表","潮流眼镜","绅士配件","DIY" };
if (position == 0) {
System.out.println("最热单品");
Intent intent1 = new Intent(this, HotActivity.class);
startActivity(intent1);
}
if (position == 1) {
System.out.println("时尚饰品");
Intent intent = new Intent(this, FashionActivity.class);
startActivity(intent);
}
if (position == 2) {
System.out.println("珠宝首饰");
Intent intent = new Intent(this, JewelryActivity.class);
startActivity(intent);
}
if (position == 3) {
System.out.println("品质手表");
Intent intent = new Intent(this, WatchActivity.class);
startActivity(intent);
}
if (position == 4) {
System.out.println("潮流眼镜");
Intent intent = new Intent(this, GlassesActivity.class);
startActivity(intent);
}
if (position == 5) {
System.out.println("绅士配件");
Intent intent = new Intent(this,GentryActivity.class);
startActivity(intent);
}
if (position == 6) {
System.out.println("DIY");
Intent intent = new Intent(HomePageActivity.this,DiyActicity.class);
startActivity(intent);
}
}
}
XML 数据格式
分享到
取消 提交回答
全部回答(2)
  • fosi.zgy
    2019-07-17 18:53:18
    检查目标activity在androidmanifest.xml中定义
    0 0
  • 爵霸
    2019-07-17 18:53:18
    //判断用户名和密码是否正确,如果正确了跳转到其他页面
    
    if(username.equals("admin")&&password.equals("123456")){
    
    Intent intent = new Intent();
    
    //AaActivity.this代表本类 HomePageActivity.class代表要跳转的类
    
    intent.setClass(AaActivity.this, HomePageActivity.class);
    
    //页面跳转
    
    startActivity(intent);
    
    Toast.makeText(AaActivity.this, "登录成功", 0).show();
    
    }else{
    
    Toast.makeText(AaActivity.this, "用户名或密码错误", 0).show();
    
    }
    0 0
添加回答
+ 订阅

大数据计算实践乐园,近距离学习前沿技术

推荐文章
相似问题
推荐课程