gpt4 book ai didi

java - Android 登录按钮停止工作

转载 作者:行者123 更新时间:2023-12-01 18:21:30 24 4
gpt4 key购买 nike

您好,我正在开发一个 Android 应用程序,但我的登录按钮无法正常工作,因为我可以使用网络服务注册,但在登录页面后它停止工作,因为我可以从注册导航到其他页面,但登录按钮不允许我登录,请帮助我,下面是我的登录功能代码

谢谢

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

btnLogin按钮的布局代码

<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" />

最佳答案

将此行更改为

Button btnLogin = (Button) findViewById(R.id.btnLogin);

到此

btnLogin = (Button) findViewById(R.id.btnLogin);

您没有正确初始化它。您正在创建新变量。如果您遇到空指针异常,这就是原因

关于java - Android 登录按钮停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com