0

Helo guys, I'm new at android programming, I'm trying to make login application connect to localhost mysql with android studio based on this web. Here is code:

Mainactivity.java

public class Mainmenu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainmenu);

    Button login=(Button)findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent login=new Intent(v.getContext(),Login.class);
            startActivity(login);
        }
    });

    Button register=(Button)findViewById(R.id.register);
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent  regis= new Intent(v.getContext(),Register.class);
            startActivity(regis);
        }
    });

    Button exit=(Button)findViewById(R.id.exit);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();

        }
    });
}

Login.java

public class Login extends ActionBarActivity {
        final EditText id=(EditText)findViewById(R.id.handphone);
        final EditText pass=(EditText)findViewById(R.id.pass_login);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

        TextView login=(TextView)findViewById(R.id.textView);
        login.setText("Login to Human Tracker");

        Button log=(Button)findViewById(R.id.login);
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=pass.getText().toString();
                String handphone=id.getText().toString();

                if (!password.equals("") && !handphone.equals("")) {
                    Toast.makeText(getApplication(),"Your id or password is wrong",Toast.LENGTH_SHORT).show();

                } else {
                    masuk();
                    Toast.makeText(getApplication(),"Welcome", Toast.LENGTH_SHORT).show();
                    Intent user = new Intent(v.getContext(), User.class);
                    startActivity(user);

                }
            }
        });
    }

    private void masuk(){

    SharedPreferences prefs;
    String prefName ="report";

    InputStream is=null;
    String result=null;
    String line=null;
    JSONObject jArray= null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("hp",id.getText().toString()));

    try {
        HttpClient httpclient=new DefaultHttpClient();
        HttpPost httppost=new HttpPost("http://10.0.0.2");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity=response.getEntity();
        is=entity.getContent();
    } catch (Exception e){
        Log.e("Fail 1: Error in HTTP connection",e.toString());
        Toast.makeText(getApplicationContext(),"Fail 1: Error in HTTP connection",Toast.LENGTH_SHORT).show();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("Fail 2: Error converting result ", e.toString());
    }

    try
    {
        JSONObject jobject = new JSONObject(result);

        String S_pwd = jobject.getString("pass");
        String S_name = jobject.getString("name");
        String S_id = jobject.getString("id");

        if(S_pwd.equals(pass.getText().toString())) {

            Toast.makeText(getBaseContext(), "Login Successfully",
                    Toast.LENGTH_SHORT).show();

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();

            //---save the values in the EditText view to preferences---
            editor.putString("id", S_id);
            editor.putString("name", S_name);

            //---saves the values---
            editor.commit();

            Toast.makeText(getApplicationContext(),"Login success",Toast.LENGTH_SHORT).show();
            }

        else {
            Toast.makeText(getBaseContext(), "Login Failure \n" +
                    "\n Try Again", Toast.LENGTH_LONG).show();

            id.setText("");
            pass.setText("");
        }
    }
    catch(Exception e)
    {
        Log.e("Fail 3", e.toString());
    }
}

Everything is okay, i can go to User.java before add that private void masuk. Then im debug my application and no error. But why when i press login button on main menu(to go Login.java) it say 'Unfortunately Login has stopped'?

Vishwajit Palankar
  • 3,033
  • 3
  • 28
  • 48
  • Please try the answer given by Stefan Beike below. If you still run into an error then copy-paste the logcat. – Gagan Apr 28 '15 at 11:39

1 Answers1

4

this will not work:

public class Login extends ActionBarActivity {
        final EditText id=(EditText)findViewById(R.id.handphone);
        final EditText pass=(EditText)findViewById(R.id.pass_login);

first of all call setContentView. After that assign the Gui elements:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText id=(EditText)findViewById(R.id.handphone);
        EditText pass=(EditText)findViewById(R.id.pass_login);

or if you need them as class members:

 EditText id;
 EditText pass;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        id=(EditText)findViewById(R.id.handphone);
        pass=(EditText)findViewById(R.id.pass_login);
nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • i have try this one, it work, but one problem appears. when i pressed button login in Login.java(`Button log=(Button)findViewById(R.id.login);`) its force close again. – Achmad Fauzi Apr 28 '15 at 15:24