gzx_vic
亚瑟龙王
2016-04-15 09:03
采纳率: 100%
浏览 3.7k

请教一个Activity跳转到另外一个Activity失败的问题

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);
}

}

}

之前可能发错了,不好意思
是不是缺少了一些代码,求补充~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

14条回答 默认 最新

  • gzx_vic
    亚瑟龙王 2016-04-21 07:44
    已采纳

    //判断用户名和密码是否正确,如果正确了跳转到其他页面

    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();
    
    }
    
    点赞 评论
  • cuper_
    AImmorta1 2016-04-15 09:09

    没有看到activity

    点赞 评论
  • cyxevil
    cyxevil 2016-04-15 09:12

    我没看到你跳转的代码啊?而且你这个粘出来的也不是你activity,这怎么看?

    点赞 评论
  • CSDNXIAOS
    Robot-S 2016-04-15 09:12

    第一个Activitypublic class activity02 extends Activity {
    /** Called when the activity is first created. */
    private Button myButton = null; //申明为成员变量
    @Override
    public void onCreate(Bundle......
    答案就在这里:从一个Activity跳转到另一个Activity
    ----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

    点赞 评论
  • denglxsc
    denglxsc 2016-04-15 09:30

    不知楼主什么意思,这里面一个intent,一个Activity都没有,怎么可能会跳转呢?你代码贴错了

    点赞 评论
  • qq_15002323
    叫我宫城大人 2016-04-15 09:57

    问问题一定要尽量说明清楚,你activity的代码都没有贴,然而把数据库的代码贴一大堆,丢一句点击登录闪退,不逗啊

    点赞 评论
  • qiaoweiya
    IT开发 2016-04-15 10:01

    你这没有intent和Activity,如何跳??

    点赞 评论
  • u014788453
    愤怒的鲨鱼 2016-04-15 10:07

    这种情况可能是没有在配置文件中注册activity

    点赞 评论
  • eyishion
    IT_凡凡 2016-04-15 10:58

    public long insertUserData(LoginData userData) {

    String userName=userData.getUserName();
    String userPwd=userData.getUserPwd();
    
    ContentValues values = new ContentValues();
    values.put(USER_NAME, userName);
    values.put(USER_PWD, userPwd);
    return mSQLiteDatabase.insert(TABLE_NAME, ID, values);
    

    }
    最后一句为啥传入ID进去吗,不是要传个null?

    点赞 评论
  • u014301041
    玉米青豆 2016-04-15 13:18

    activity没找到,我还以为我看错了呢,

    点赞 评论
  • chen_zhang_yu
    chen_zhang_yu 2016-04-16 00:46

    贴了一大片数据库的代码,问题都没描述清楚

    点赞 评论
  • baidu_23086307
    好一个大布丁 2016-04-16 02:09
    点赞 评论
  • qq_27578023
    qq_27578023 2016-05-30 13:08

    //判断用户名和密码是否正确,如果正确了跳转到其他页面

    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();

    }

    点赞 评论
  • qq_27578023
    qq_27578023 2016-06-01 15:24

    //判断用户名和密码是否正确,如果正确了跳转到其他页面

    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();

    }

    点赞 评论

相关推荐