0

这是我的 MainActivity.java 文件。我用:

  io login_io = new io(this);

调用我的 io.java 类文件。

第二个声明我应该使用什么而不是“this”?(在代码中注释掉)

 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;

                }
        }
    });

    }

}
4

2 回答 2

1

您需要传入一个 context ,这是您在第一种情况下所做的。

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

有关如何将上下文传递给 onclick 的示例 在 onClick(DialogInterface v, int buttonId) 中获取上下文?

于 2013-03-21T03:05:49.893 回答
0

尝试给予MainActivity.this而不是仅仅给予this。在第一种情况下,声明在Activity类中,因此,this它是一个属性Context。但在第二种情况下,您需要显式指定Context,这可以通过给 来实现MainActivity.this

此外,如果您更改此变量的名称以获得更好的可读性,那就太好了。

于 2013-03-21T03:05:24.043 回答