0

Here is my MainActivity.java file. I use:

  io login_io = new io(this);

To call my io.java class file.

What should I be using instead of 'this' for the second declaration? (commented out in code)

 package com.myschedules;

 //FILE I/O
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;

 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;

 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.EditText;
 import android.widget.ProgressBar;
 import android.widget.Toast;


 public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toast("asdf");

            //Works fine
    io login_io = new io(this);

    //get the controls we created in activity_main.xml, also with the resource reference and the id
    final EditText txt_username = (EditText)findViewById(R.id.txt_username);
    final EditText txt_password = (EditText)findViewById(R.id.txt_password);
    final Button cmd_login = (Button)findViewById(R.id.cmd_login);
    final ProgressBar pb_login = (ProgressBar)findViewById(R.id.pb_login);
    final CheckBox chk_rememberme = (CheckBox)findViewById(R.id.chk_rememberme);
    final CheckBox chk_loginautomatically = (CheckBox)findViewById(R.id.chk_loginautomatically);

    //Load LOG IN PREFERENCES
    final String login_preferences = login_io.load("login_preferences.dat");
    final String username = login_io.load("username.dat");
    final String password = login_io.load("password.dat");

        if (login_preferences.charAt(0) == '1'){    //REMEMBER ME

            chk_rememberme.setChecked(true);    //set CHECK BOX
            txt_username.setText(username);     //set USER NAME
            txt_password.setText(password);     //set PASSWORD
        }

        if (login_preferences.charAt(1) == '1'){    //LOG ME IN AUTOMATICALLY
            chk_loginautomatically.setChecked(true);

            //COPY the OnClick function operations here
            pb_login.setVisibility(1);  //PROGRESS BAR set to VISIBLE
            cmd_login.setText("Logging in..."); //CHANGE the LOG IN BUTTON text to display logging in status
           // cmd_login.setEnabled(false);  //DISABLE the LOG IN BUTTON from being able to be clicked
        }


    //add new KeyListener Callback (to record key input)
    cmd_login.setOnClickListener(new OnClickListener()
    {
                    //ERROR HERE! (due to 'this', i think?)
        io login_io = new io(this);

        public void onClick(View v) 
        {

            String login_preferences = login_io.load("login_preferences.dat");
            final int id = v.getId();
            switch (id) 
                {

                //if LOG IN BUTTON is clicked
                case R.id.cmd_login:

                    pb_login.setVisibility(1);  //PROGRESS BAR set to VISIBLE
                    cmd_login.setText("Logging in..."); //CHANGE the LOG IN BUTTON text to display logging in status
                    cmd_login.setEnabled(false);    //DISABLE the LOG IN BUTTON from being able to be clicked



                    //REMEMBER ME PREFERENCES
                    if(chk_rememberme.isChecked()){
                        login_preferences = "1";    //set CHK_REMEMBERME to TRUE

                        //if REMEMBER ME is checked, save USERNAME and PASSWORD
                        String user_name = txt_username.getText().toString();   //USER NAME
                        login_io.save("username.dat",user_name);    //save USERNAME

                        String password = txt_password.getText().toString();    //PASSWORD
                        login_io.save ("password.dat",password);    //save PASSWORD

                    }else{
                        //otherwise do not store a USERNAME or PASSWORD
                        login_preferences = "0";    //set CHK_REMEMBERME to FALSE
                        login_io.save ("username.dat","");  //reset USERNAME
                        login_io.save ("password.dat","");  //reset PASSWORD
                    }

                    // LOG IN AUTOMATICALLY PREFERENCES
                    if(chk_loginautomatically.isChecked()){
                        login_preferences = login_preferences + "1";    //set to TRUE
                    }else{
                        login_preferences = login_preferences + "0";    //set to FALSE
                    }

                    login_io.save("login_preferences.dat",login_preferences);   //save LOG IN AUTOMATICALLY PREFERENCES

                    Intent test = new Intent(MainActivity.this, Calendar.class);
                    startActivity(test);      

                    break;

                }
        }
    });

    }

}
Danny Delott
  • 6,756
  • 3
  • 33
  • 57

2 Answers2

1

You need to pass in a context , which is what you are doing in the first case.

http://developer.android.com/reference/android/content/Context.html

An example on how to pass Context to your onclick Get context inside onClick(DialogInterface v, int buttonId)?

Community
  • 1
  • 1
smk
  • 5,340
  • 5
  • 27
  • 41
0

Try giving MainActivity.this instead of only this. In the first case, the declaration was in the Activity class, and hence, this served as a proper Context to that. But in the second case, you need to explicitly specify the Context, which can be achieved by giving MainActivity.this.

Also, it'll be nice if you change the name of this variable for better readability.

Rahul
  • 44,383
  • 11
  • 84
  • 103