我想做什么
我试图创建一个在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)
如果您需要更多代码或更多详细信息,请在下面的注释中编写。
最佳答案
getApplicationContext();
对于对话框或任何其他UI更改功能,您必须从活动中传递。
仅当不涉及UI(加载资源,首选项等)时,才应使用getApplicationContext()。