Hi I am developing an android app, but my login button
not working properly, as I am able to signup with web service
but after in login page its stopped working, as I can navigate from signup to other page but login button does not let me login, please help me, below is my code for login functionality
thanks
public class Login extends ActionBarActivity {
Button btnLogin;
Button fbbutton;
Button gpbutton;
Button twbutton;
Button btnRegister;
EditText username, password;
String uname, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
//import button
Button btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent i = new Intent(getApplicationContext(),
Signup.class);
startActivity(i);
finish();
}
});
}
public void send(View v) {
try {
// CALL post method to make post method call
post();
} catch (Exception ex) {
String error = ex.getMessage();
}
}
//Method to get list value pair and form the query
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
//Method to post data to webservice
public void post() throws UnsupportedEncodingException {
try {
// Calling async task to get json
new DownloadOperation().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
//Handle popout messages
public void error(boolean flag, String etext) {
if (flag == true) {
Toast.makeText(getBaseContext(), etext, Toast.LENGTH_SHORT).show();
//Code to handle failure
username.setText("");
password.setText("");
} else {
Toast.makeText(getBaseContext(), etext, Toast.LENGTH_SHORT).show();
setContentView(R.layout.welcome);
}
}
private class DownloadOperation extends AsyncTask<Void, Void, String> {
String uname = "";
String pass = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
// Get user defined values
uname = username.getText().toString();
pass = password.getText().toString();
}
@Override
protected String doInBackground(Void... params) {
String response = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://rgbpallete.in/led/api/login");
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpResponse = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
return response;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String jsonStr) {
super.onPostExecute(jsonStr);
Log.d("tag", "Result:\n" + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String message = jsonObj.getString("message");
boolean error = jsonObj.getBoolean("error");
error(error, message);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
layout code of btnLogin
Button
<Button
android:id="@+id/btnLogin"
style="@android:style/Widget.Material.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/button"
android:text="Login"
android:textColor="#ffffff"
android:textSize="20sp"
android:onClick="welcome" />
change this line to
Button btnLogin = (Button) findViewById(R.id.btnLogin);
to this
btnLogin = (Button) findViewById(R.id.btnLogin);
you are not initialize it properly.you are creating new variable.if you are getting null pointer exception that would be the reason
Your application is crashing because in xml you have set onClick
as welcome
(as per your comment). But you haven't implement it in Activity
. So, when you click on that Button you are getting java.lang.NoSuchMethodException
. In Activity
welcome
method is required add this in Activity
public void welcome(View v) {
//do your action i.e call AsyncTask
}
make sure you have registered onclick listener to your login button, you haven't registered any click listener with btnLogin and also not set any action to be performed onClick to btnLogin!! just implemented those in btnRegister! I think you forgot it!!
if you are using onclick attribute of xml; then make sure these things are correct regarding your method:
a. your method is public and is the member of the Activity.
b. corresponding method should have one parameter, whose type should match the XML object. eg: View
You are missing
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin .setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//all your stuff here
}
});
Add and test.. Actually for each button
you have to use OnClickListener
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.