Android登录按钮停止工作

时间:2014-12-24 08:15:23

标签: java android android-activity

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

谢谢

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

4 个答案:

答案 0 :(得分:2)

将此行更改为

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

到这个

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

你没有正确初始化它。你正在创建新的变量。如果你得到空指针异常那将是原因

答案 1 :(得分:2)

您的应用程序崩溃了,因为在xml中您已将onClick设置为welcome(根据您的评论)。但是你还没有在Activity中实现它。因此,当您点击该按钮时,您将获得java.lang.NoSuchMethodException。在Activity welcome方法中需要在Activity

中添加此方法
public void welcome(View v) {
   //do your action i.e call AsyncTask
}

答案 2 :(得分:0)

  1. 确保您已在登录按钮上注册了onclick监听器,您尚未使用 btnLogin 注册任何点击监听器,也未设置任何要执行的操作onClick to btnLogin !!刚刚在btnRegister中实现了那些!我想你忘记了!!

  2. 如果您使用的是xml的onclick属性;然后确保这些方法对于您的方法是正确的:

  3. 一个。您的方法是公开的,并且是活动的成员。

    湾对应的方法应该有一个参数,其类型应该与XML对象匹配。例如:查看

答案 3 :(得分:0)

你错过了

按钮btnLogin =(按钮)findViewById(R.id.btnLogin);

    btnLogin .setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            //all your stuff here
        }
    });

添加并测试..实际上,对于每个button,您必须使用OnClickListener