我做一个Android页面。在此页面中有一些产品目录。客户可以查找产品,如果要购买,可以按订购按钮。如果客户未登录。它将跳转到登录页面。
但是当我将登录页面添加到目录页面时,会给我一个错误。我该怎么办?
这是我的代码MainActivity.java
public class MainActivity extends Activity {
ArrayList<Team> teams = new ArrayList<Team>();
ListView listview;
String account="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
if(!bundle.getString("ACCOUNT").equals(null) ){
account=bundle.getString("ACCOUNT");
}
listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(lvonclick);
String result =dbcourse.executeQuery();
Team team =new Team("my_product","price");
teams.add(team);
team = new Team("fish", "77");
teams.add(team);
final TeamsAdapter adapter = new TeamsAdapter(this, R.layout.team, teams);
listview.setAdapter(adapter);
}
private ListView.OnItemClickListener lvonclick=
new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String sel=parent.getItemAtPosition(position).toString();
mydialog(sel);
}
};
private void mydialog(String str1){
new AlertDialog.Builder(MainActivity.this)
.setTitle("product")
.setIcon(R.drawable.ic_launcher)
.setMessage(str1)
.setPositiveButton("buy", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i)
{
if(account==""){
Intent intent2=new Intent();//jump to login page
intent2.setClass(MainActivity.this,Login.class);
startActivity(intent2);
}
else{mytoast("success");}
}
})
.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
login.java:
public class Login extends Activity {//登入畫面
private TextView acc,pwd;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
acc=(TextView)findViewById(R.id.acc);
pwd=(TextView)findViewById(R.id.pwd);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(btnlogin);
}
private Button.OnClickListener btnlogin=new Button.OnClickListener(){
public void onClick(View v){
String account=acc.getText().toString();
String passwd=pwd.getText().toString();
Intent intent= new Intent();
intent.setClass(Login.this, MainActivity.class);
Bundle bundle=new Bundle();
if(account=="123")
{
bundle.putString("ACCOUNT", account);
intent.putExtras(bundle);
startActivity(intent);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
我认为您上面提到的if(!bundle.getString(“ ACCOUNT”)。equals(null))方法是错误的检查方法。我总是喜欢这样
if (intent != null && intent.getExtras() != null) {
mCurrentUser = new User();
mCurrentUser.setmEmail(intent.getExtras().getString(Email));
}
答案 1 :(得分:0)
正常的检查登录逻辑如下所示
但是你的逻辑是
MainActivity -> LoginActivity (login success) -> (start a new) MainActivity
您有两个MainActivity。
这是代码演示
MainActivity {
dialogButtonClick{
//startActivityForResult() go to LoginActivity
}
onActivityResult() {
//check result here
}
}
LoginActivity {
loginButtonClick() {
//login success
//setResultCode
//set ACCOUNT info to the intent
//finish() current activity
}
}
如果您不了解startActivityForResult()
和onActivityResult()
逻辑,请参考文档click here