我是移动应用程序开发的初学者,目前我正在进行登录(在线数据库)和Register.I想通过使用putExtra和getStringExtra将一些参数传递给下一个活动。以下是我的两项活动代码:
登录:
的
的public class Login extends AppCompatActivity implements View.OnClickListener {
Button bLogin;
TextView registerLink;
EditText etUsername, etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
bLogin = (Button) findViewById(R.id.bLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
registerLink = (TextView) findViewById(R.id.tvRegisterLink);
bLogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bLogin:
String Username = etUsername.getText().toString();
String Password = etPassword.getText().toString();
Toast.makeText(this, "Loginin...", Toast.LENGTH_SHORT).show();
new Authenticate(this).execute(Username, Password);
break;
case R.id.tvRegisterLink:
Intent registerIntent = new Intent(Login.this, Register.class);
startActivity(registerIntent);
break;
}
}
public void ParentLogin()
{
String Username = etUsername.getText().toString();
String Password = etPassword.getText().toString();
Intent Main = new Intent(Login.this, MainActivity.class);
Main.putExtra("class","Parent");
Main.putExtra("username",Username);
Main.putExtra("password",Password);
startActivity(Main);
}
public void NannyLogin()
{
String Username = etUsername.getText().toString();
String Password = etPassword.getText().toString();
Intent Main = new Intent(Login.this, MainActivity.class);
Main.putExtra("class", "Nanny");
Main.putExtra("username",Username);
Main.putExtra("password",Password);
startActivity(Main);
}
public class Authenticate extends AsyncTask<String, Void, String> {
private Context context;
public Authenticate(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
String Username = arg0[0];
String Password = arg0[1];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?username=" + URLEncoder.encode(Username, "UTF-8");
data += "&password=" + URLEncoder.encode(Password, "UTF-8");
link = "http://juliusgoh.comxa.com/Authenticate.php" + data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return ("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObj = new JSONObject(result);
String query_result = jsonObj.getString("query_result");
switch (query_result) {
case "PSUCCESS":
Toast.makeText(context, "Login Successfully....Parent", Toast.LENGTH_SHORT).show();
ParentLogin();
break;
case "NSUCCESS":
Toast.makeText(context, "Login Successfully....Nanny", Toast.LENGTH_SHORT).show();
NannyLogin();
break;
case "FAILURE":
Toast.makeText(context, "Wrong Data. Login failed.", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
break;
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
}
的
第二项活动:
的
的公共类MainActivity扩展了AppCompatActivity {
public String job;
public String username;
public String password;
TextView qwe;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
qwe = (TextView)findViewById(R.id.asd);
Intent intent = new Intent();
String strValue = intent.getStringExtra("class");
String strValue1 = intent.getStringExtra("username");
String strValue2 = intent.getStringExtra("password");
qwe.setText(strValue);
}
的
代码工作正常,但在第二个活动中它没有显示任何内容。我猜它通过&#34;&#34; null argument.But我不知道如何解决它。请提供一些建议。 TQ <3
答案 0 :(得分:0)
我找到了问题的解决方案,我的第二个活动应该输入Intent intent = getIntent();而不是Intent intent = new Intent;