Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:11:24+00:00 2026-05-31T16:11:24+00:00

What I’m trying to do I’m trying to create a Login which is fully

  • 0

What I’m trying to do


I’m trying to create a Login which is fully handled in a Helperclass, to keep the code simple.

For this I created a helperclass with all the functionality I need, now the Problem is that I always get a NullPointer-Exception, probably because of the getApplicationContext.

Question


So what do I need to change in my Code to get this working. The code is provided below. If you have a great tutorial or something please provide me this. I really like to do this on my own.

Code


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)

If you need more code, or more detailed information just write it in the comments below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:11:25+00:00Added an answer on May 31, 2026 at 4:11 pm
    getApplicationContext(); 
    

    For dialogs or any other UI altering functions you have to pass this from your activity.

    getApplicationContext() should be used only when the UI is not involved (loading resources, prefs, .. etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.