我正在尝试做什么
我正在尝试创建一个在Helperclass
中完全处理的Login,以保持代码简单。
为此,我创建了一个helperclass
,其中包含我需要的所有功能,现在问题是我总是得到NullPointer-Exception
,可能是因为getApplicationContext
。
问题
那么我需要在我的代码中进行哪些更改才能实现此功能。代码如下。如果您有一个很棒的教程或者其他什么,请提供给我。我真的很想自己做这件事。
代码
LoginActivity.class
package de.ivocore;
public class LoginActivity extends Activity {
private DBAdapter mDbHelper;
private LoginHelper login = new LoginHelper();
**/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mDbHelper = new DBAdapter(this);
mDbHelper.open();
mDbHelper.close();
// Text-Felder
final EditText user = (EditText) findViewById(R.id.edt_username);
final EditText pwd = (EditText) findViewById(R.id.edt_password);
// Button
Button loginbt = (Button) findViewById(R.id.bt_login);
// OnClickListner für den Button definieren
loginbt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Eingegebener Username und Passwort auslesen
final String usertxt = user.getText().toString();
final String pwdtxt = pwd.getText().toString();
Log.d("User", usertxt);
Log.d("PWD", pwdtxt);
checkLoginData(usertxt, pwdtxt);
}
});
}
public void checkLoginData(final String username, final String password){
boolean user, pass;
if (username.length() < 6){
user = false;
}else {
user = true;
}
if (password.length() < 6){
pass = false;
}else {
pass = true;
}
int val = 0;
if (user) val |= 0x1;
if (pass) val |= 0x2;
switch (val){
case 0:
login.loginFalse(getApplicationContext(),"Username und Passwort müssen mindestens 6 Zeichen haben");
break;
case 1:
login.loginFalse(getApplicationContext(),"Passwort muss mindestens 6 Zeichen haben");
break;
case 2:
login.loginFalse(getApplicationContext(),"Username muss mindestens 6 Zeichen haben");
break;
case 3:
login.doLogin(getApplicationContext(),username, password);
}
}
}
LoginHelper.class
package de.ivocore.firstboot;
public class LoginHelper {
private static final String PREFS_NAME = "de.ivocore.firstboot.PREF_NAME";
private static final String PREFS_USER = "de.ivocore.firstboot.PREF_USER";
private static final String PREF_FIRSTBOOT = "de.ivocore.firstboot.FIRSTBOOT";
ProgressDialog progressDialog;
private static SharedPreferences mPrefs;
boolean first;
Context test;
// Handlet die Message aus dem Thread
private Handler handler = new Handler() {
public void handleLogin(Message msg) {
switch(msg.what){
case 0: {
progressDialog.dismiss();
Intent i = new Intent(test, MainActivity.class);
test.startActivity(i);
} break;
case 1:{
progressDialog.dismiss();
String text = "Username oder Passwort inkorrekt";
loginFalse(test, text);
} break;
}
}
};
/**
* Hier wird ein AlertDialog erzeugt welcher zurück gibt ob was am Login
* falsch war.
*
* @param ctx
* Context der Klasse
* @param text
* Text der ausgegeben werden soll
*/
public void loginFalse(Context ctx, String text) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(ctx)
.setMessage(text)
.setTitle("Login inkorrekt")
.setCancelable(true)
.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).show();
}
public void doLogin(final Context ctx, final String username,
final String password) {
progressDialog = ProgressDialog.show(ctx, "Login", "Bitte warten...");
Thread t = new Thread() {
@Override
public void run() {
String URL = "http://LOGINURL (NotForYourEyes)"
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response;
try {
//Firstboot überprüfung
HttpPost post = new HttpPost(URL);
StringEntity se = new StringEntity("username=" + username
+ "&password=" + password);
post.setEntity(se);
post.addHeader("Content-Type",
"application/x-www-form-urlencoded"); // Dieser
// Header is
// von curl
// -d Befehl
// abgeleitet
response = client.execute(post);
int statuscode = response.getStatusLine().getStatusCode();
switch (statuscode) {
case 200:
if (response != null) {
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
String result;
result = sb.toString();
// User-Id auslesen
final String uid;
JSONObject holder = new JSONObject(result);
uid = holder.getString("id");
Log.d("JSON-Response", uid);
String token = "test-token";
SharedPreferences logindata = ctx.getSharedPreferences(PREFS_USER, 0);
SharedPreferences.Editor editor = logindata.edit(); editor.clear();
editor.putString("userid", uid);
editor.putString("token", token);
editor.commit();
handler.sendEmptyMessage(0);
}
break;
case 500:
handler.sendEmptyMessage(1);
break;
}
} catch (Exception e) {
e.printStackTrace();
Log.e("DataHandler", "URLConnection-Error" + e);
}
}
};
t.start();
}
/**
* Mit dieser Methode wird heraussgesucht ob der erste Login bereits
* geschehen ist
*
* @return firstboot
*/
private boolean getFirstboot() {
boolean firstboot = mPrefs.getBoolean(PREF_FIRSTBOOT, first);
return firstboot;
}
/**
* Hier wird die Variabel auf true gesetzt, falls der Firstboot bereits
* passiert ist.
*
* @param first
*/
private static void setFirstboot(boolean first) {
mPrefs.edit().putBoolean(PREF_FIRSTBOOT, first).commit();
}
}
logcat的:
03-14 08:12:53.464: E/AndroidRuntime(9462): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.ViewRootImpl.setView(ViewRootImpl.java:519)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.app.Dialog.show(Dialog.java:274)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
03-14 08:12:53.464: E/AndroidRuntime(9462): at de.ivocore.firstboot.LoginHelper.loginFalse(LoginHelper.java:80)
03-14 08:12:53.464: E/AndroidRuntime(9462): at de.ivocore.LoginActivity.checkLoginData(LoginActivity.java:120)
03-14 08:12:53.464: E/AndroidRuntime(9462): at de.ivocore.LoginActivity$1.onClick(LoginActivity.java:92)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.View.performClick(View.java:3480)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.view.View$PerformClick.run(View.java:13983)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.os.Handler.handleCallback(Handler.java:605)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.os.Looper.loop(Looper.java:137)
03-14 08:12:53.464: E/AndroidRuntime(9462): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-14 08:12:53.464: E/AndroidRuntime(9462): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 08:12:53.464: E/AndroidRuntime(9462): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 08:12:53.464: E/AndroidRuntime(9462): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-14 08:12:53.464: E/AndroidRuntime(9462): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-14 08:12:53.464: E/AndroidRuntime(9462): at dalvik.system.NativeStart.main(Native Method)
如果您需要更多代码或更详细的信息,请在下面的评论中写下来。
答案 0 :(得分:1)
getApplicationContext();
对于对话框或任何其他UI更改功能,您必须从活动中传递此功能。
只有在不涉及UI(加载资源,首选项等等)时才应使用getApplicationContext()。
答案 1 :(得分:1)
试试这个
View view = LayoutInflater.from(getParent()).inflate(
R.layout.login, null);
setContentView(view);
我用这个
解决了我自己的问题答案 2 :(得分:0)
尝试初始化LoginHelper。在当前代码中,它将为null。