gpt4 book ai didi

适用于Android的JAVA : improper use of "this" constructor causing issues with external class method calls

转载 作者:行者123 更新时间:2023-12-01 14:44:51 25 4
gpt4 key购买 nike

这是我的 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;

}
}
});

}

}

最佳答案

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

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

有关如何将上下文传递给 onclick 的示例 Get context inside onClick(DialogInterface v, int buttonId)?

关于适用于Android的JAVA : improper use of "this" constructor causing issues with external class method calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15538716/

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