我有一个简单的PHP登录表单的Android代码,但我收到以下错误,请帮助他解决我犯的错误。 PS:我是Android新手。
Error:(34, 9) error: cannot find symbol variable UserLocalStore
Error:(37, 38) error: incompatible types: <null> cannot be converted to int
Error:(38, 31) error: non-static method storeUserData(User) cannot be referenced from a static context
Error:(39, 31) error: non-static method setUserLoggedIn(boolean) cannot be referenced from a static context
我的代码如下,
Login.java
package techcarty.techcarty.com.techcarty;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends AppCompatActivity {
Button bLogin;
EditText etMobileno, etPassword;
TextView register,fpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etMobileno=(EditText)findViewById(R.id.etMobileno);
etPassword=(EditText)findViewById(R.id.etPassword);
bLogin=(Button)findViewById(R.id.bLogin);
register=(TextView)findViewById(R.id.register);
fpass=(TextView)findViewById(R.id.fpass);
UserLocalStore = new UserLocalStore(this);
bLogin.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
User user = new User(null, null);
UserLocalStore.storeUserData(user);
UserLocalStore.setUserLoggedIn(true);
}
});
}
public void onClick(View iq) {
switch (iq.getId()) {
case R.id.register:
startActivity(new Intent(this, Register.class));
break;
case R.id.fpass:
//toast text begins
Context context = getApplicationContext();
CharSequence text = "Still need to develop";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//toast text ends
break;
}
}
}
此外,我的UserLocalStore.java文件包含
package techcarty.techcarty.com.techcarty;
package techcarty.techcarty.com.techcarty;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends AppCompatActivity {
Button bLogin;
EditText etMobileno, etPassword;
TextView register,fpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etMobileno=(EditText)findViewById(R.id.etMobileno);
etPassword=(EditText)findViewById(R.id.etPassword);
bLogin=(Button)findViewById(R.id.bLogin);
register=(TextView)findViewById(R.id.register);
fpass=(TextView)findViewById(R.id.fpass);
UserLocalStore = new UserLocalStore(this);
bLogin.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
User user = new User(null, null);
UserLocalStore.storeUserData(user);
UserLocalStore.setUserLoggedIn(true);
}
});
}
public void onClick(View iq) {
switch (iq.getId()) {
case R.id.register:
startActivity(new Intent(this, Register.class));
break;
case R.id.fpass:
//toast text begins
Context context = getApplicationContext();
CharSequence text = "Still need to develop";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
//toast text ends
break;
}
}
}
l
import android.content.Context;l
import android.content.SharedPreferences;l
l
/**l
* Created by SI on 3/18/2016.l
*/l
public class UserLocalStore {l
public static final String SP_NAME = "userDetailesl";
SharedPreferences userLocalDatabase;l
l
public UserLocalStore(Context context){l
userLocalDatabase = context.getSharedPreferencles(SP_NAME, 0);
l
l
}l
public void storeUserData(User user){l
SharedPreferences.Editor spEditor = userLocalDlatabase.edit();
spEditor.putString("name", user.name);l
spEditor.putInt("age",user.mobno);l
spEditor.putString("password", user.password);l
spEditor.commit();l
}l
public User getLoggedInUser(){l
String name = userLocalDatabase.getString("namle", "");
int mobno = userLocalDatabase.getInt("mobno", l-1);
String password = userLocalDatabase.getString(l"password", "");
l
User storedUser = new User(name, password, moblno);
return storedUser;l
}l
public void setUserLoggedIn(boolean loggedIn){l
SharedPreferences.Editor spEditor = userLocalDlatabase.edit();
spEditor.putBoolean("loggedIn", loggedIn);l
spEditor.commit();l
}l
public boolean getUserLoggedIn(boolean loggedIn){l
if(userLocalDatabase.getBoolean("loggedIn", fallse) == true){
return true;l
}else{l
return false;l
}l
l
}l
public void clearUserData(){l
SharedPreferences.Editor spEditor = userLocalDlatabase.edit();
spEditor.clear();l
spEditor.commit();l
}l
}l
l
答案 0 :(得分:0)
在创建对象
时为UserLocalStore指定变量名称