强制关闭应用程序

时间:2012-03-22 17:02:19

标签: android

我不知道为什么,但我的应用程序突然一直强行关闭我。它早些时候工作正常。

请看一下。

public class AgentPortalActivity extends Activity implements OnClickListener {

String username;
String password;
Button login;
EditText uid;
EditText pwd;
CheckBox chkRememberPassword;
SharedPreferences loginPreferences;
SharedPreferences.Editor loginPrefsEditor;
Boolean saveLogin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // User Id and Password Text Fields
    uid = (EditText) findViewById(R.id.uid);
    pwd = (EditText) findViewById(R.id.pw);

    // Converts the Text in the User Id and Password field to Strings
    username = uid.getText().toString();
    password = pwd.getText().toString();

    // Login Button On Click Listener
    login = (Button) findViewById(R.id.login);
    login.setOnClickListener(this);

    // Remember Password Save Properties
    chkRememberPassword = (CheckBox) findViewById(R.id.chkRememberPassword);
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
    loginPrefsEditor = loginPreferences.edit();

    saveLogin = loginPreferences.getBoolean("saveLogin", false);

    if (saveLogin == true) {
        uid.setText(loginPreferences.getString(username, ""));
        pwd.setText(loginPreferences.getString(password, ""));
        chkRememberPassword.setChecked(true);
        // End

    }
}

public void onClick(View view) {
    if (view == login) {

        // Loading Dialog Box
        final ProgressDialog dialog = new ProgressDialog(
                AgentPortalActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Logging In...");
        dialog.show();

        Thread t = new Thread(new Runnable() {
            public void run() {
                while (dialog.getProgress() < dialog.getMax()) {

                    try {

                        Thread.sleep(50);
                    } catch (Exception e) {/* no-op */
                    }
                }
                dialog.dismiss();
            }
        });
        t.start();
        // End Of Loading Dialog Box

        // Hides Soft Keyboard
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(uid.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(pwd.getWindowToken(), 0);

        // Button OnClick Vibrating Service
        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(50);

        // Send username and password strings into screen1 class
        Intent i = new Intent(this, screen1.class);
        i.putExtra("uid", username);
        i.putExtra("pwd", password);
        startActivity(i);
        // End

        if (chkRememberPassword.isChecked()) {
            loginPrefsEditor.putBoolean("saveLogin", true);
            loginPrefsEditor.putString(username, username);
            loginPrefsEditor.putString(password, password);
            loginPrefsEditor.commit();
        } else {
            loginPrefsEditor.clear();
            loginPrefsEditor.commit();
        }

        doSomethingElse();
    }
}

public void doSomethingElse() {

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);

}

}

继承我的第二堂课

public class screen1 extends Activity implements View.OnClickListener {

String username;
String password;

// EditTexts
final EditText milein = (EditText) findViewById(R.id.milein);
final EditText zipin = (EditText) findViewById(R.id.zipin);
final EditText mileout = (EditText) findViewById(R.id.mileout);
final EditText zipout = (EditText) findViewById(R.id.zipout);
final EditText feed = (EditText) findViewById(R.id.feed);

// TextViews
final TextView dispatch = (TextView) findViewById(R.id.dispatch);
final TextView dispatchnum = (TextView) findViewById(R.id.dispatchnum);
final TextView csr = (TextView) findViewById(R.id.csr);
final TextView csrnum = (TextView) findViewById(R.id.csrnum);
final TextView it = (TextView) findViewById(R.id.it);
final TextView itnum = (TextView) findViewById(R.id.itnum);
final TextView thoughts = (TextView) findViewById(R.id.thoughts);
final TextView information = (TextView) findViewById(R.id.information);

//ToggleButtons
final ToggleButton checkinbutton = (ToggleButton) findViewById(R.id.checkinbutton);

// Buttons
final Button submit = (Button) findViewById(R.id.submit);
final Button send = (Button) findViewById(R.id.send);


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

    // Calls String from AgentPortalActivity class to pass through to
    // screen1 class
    Intent i = getIntent();
    username = i.getExtras().getString("uid");
    password = i.getExtras().getString("pwd");
    // End

    // Passes username and password strings and inserts into URL for login
    // and pwd
    JSONObject json = JSONfunction
            .getJSONfromURL("http://whatever.com/api/?type=json&uid="
                    + (username) + "&pwd=" + (password));
    // End

    try {

        // boolean suspended = json.getBoolean("suspended");
        String firstName = json.getString("firstName");
        String lastName = json.getString("lastName");
        // int checkdin = json.getInt("checkedin");
        // int checkindatetime = json.getInt("checkindatetime");

        // Get Json object address
        JSONObject address = json.getJSONObject("address");
        String streetAddress = address.getString("streetAddress");
        String city = address.getString("city");
        String state = address.getString("state");
        String postalCode = address.getString("postalCode");
        // End

        // Get PhoneNumber Array
        JSONArray phoneNumbers = json.getJSONArray("phoneNumber");
        // String type = phoneNumbers.getJSONObject(0).getString("type");
        String number = phoneNumbers.getJSONObject(0).getString("number");
        // End

        information.setText("Hello," + " " + (firstName) + " " + (lastName)
                + "!" + " " + "Your Home Number is" + " " + (number) + " "
                + "Your Address is" + " " + (streetAddress) + " " + (city)
                + " " + (state) + " " + (postalCode));

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    /** Submit Button Invisible Child Properties */
    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);

            // Submit Mile-IN Mile-Out Button
            if (submit.isPressed()) {
                milein.setVisibility(View.GONE);
                zipin.setVisibility(View.GONE);
                mileout.setVisibility(View.GONE);
                zipout.setVisibility(View.GONE);
                submit.setVisibility(View.GONE);
                dispatch.setVisibility(View.GONE);
                dispatchnum.setVisibility(View.GONE);
                csr.setVisibility(View.GONE);
                csrnum.setVisibility(View.GONE);
                it.setVisibility(View.GONE);
                itnum.setVisibility(View.GONE);
                thoughts.setVisibility(View.GONE);
                feed.setVisibility(View.GONE);
                send.setVisibility(View.GONE);
                // End

                // Button OnClick Vibrating Service
                Vibrator vib1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                vib1.vibrate(50);
                // End

                // Toast "Please Wait" Upon Clicking Submit Button
                Toast.makeText(getBaseContext(), "Please Wait...",
                        Toast.LENGTH_SHORT).show();
                // End

            }
        }
    });

    // Boolean for Check-In Button Sharedpreferences
    final SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean tgpref = preferences.getBoolean("tgpref", false);
    checkinbutton.setChecked(tgpref);
    // End

    // ToggleButton Check-IN
    checkinbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);
            // End

            // Only Displays Mile-in Zip-in and Submit Button
            if (checkinbutton.isChecked()) {
                milein.setVisibility(View.VISIBLE);
                zipin.setVisibility(View.VISIBLE);
                submit.setVisibility(View.VISIBLE);
                mileout.setVisibility(View.GONE);
                zipout.setVisibility(View.GONE);
                dispatch.setVisibility(View.GONE);
                dispatchnum.setVisibility(View.GONE);
                csr.setVisibility(View.GONE);
                csrnum.setVisibility(View.GONE);
                it.setVisibility(View.GONE);
                itnum.setVisibility(View.GONE);
                thoughts.setVisibility(View.GONE);
                feed.setVisibility(View.GONE);
                send.setVisibility(View.GONE);
                // End

                // Button OnClick Vibrating Service
                Vibrator vib1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                vib1.vibrate(50);
                // End

                Toast.makeText(getBaseContext(), "Checking In...",
                        Toast.LENGTH_SHORT).show();

                // Boolean Preferences for Checkinbutton ToggleButton
                // Remembers Toggle State Checked
                SharedPreferences.Editor editor = preferences.edit();
                final SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                boolean tgpref = preferences.getBoolean("tgpref", false);
                checkinbutton.setChecked(tgpref);
                editor.putBoolean("tgpref", true);
                editor.commit();
                // End

            } else {

                mileout.setVisibility(View.VISIBLE);
                zipout.setVisibility(View.VISIBLE);
                submit.setVisibility(View.VISIBLE);
                milein.setVisibility(View.GONE);
                zipin.setVisibility(View.GONE);
                dispatch.setVisibility(View.GONE);
                dispatchnum.setVisibility(View.GONE);
                csr.setVisibility(View.GONE);
                csrnum.setVisibility(View.GONE);
                it.setVisibility(View.GONE);
                itnum.setVisibility(View.GONE);
                thoughts.setVisibility(View.GONE);
                feed.setVisibility(View.GONE);
                send.setVisibility(View.GONE);

                // Boolean Preferences for Checkinbutton ToggleButton
                // Remembers Toggle State Un-Checked
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("tgpref", false);
                editor.commit();
                // End

                // AlertDialog Meal-Periods Button Configurations
                final AlertDialog alertDialog = new AlertDialog.Builder(
                        screen1.this).create();
                alertDialog
                        .setMessage("I hereby verify that I have received all my 10 minute rest periods and 30 minute meal breaks today.");
                alertDialog.setButton("Agree",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Toast.makeText(getBaseContext(),
                                        "Checking Out...",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                alertDialog.setButton2("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Toast.makeText(
                                        getBaseContext(),
                                        "Please contact Ro*** Mu*** (Human Resource) to report any discrepancies",
                                        Toast.LENGTH_LONG).show();
                                checkinbutton.setChecked(true);
                                milein.setVisibility(View.GONE);
                                zipin.setVisibility(View.GONE);
                                mileout.setVisibility(View.GONE);
                                zipout.setVisibility(View.GONE);
                                submit.setVisibility(View.GONE);
                                dispatch.setVisibility(View.GONE);
                                dispatchnum.setVisibility(View.GONE);
                                csr.setVisibility(View.GONE);
                                csrnum.setVisibility(View.GONE);
                                it.setVisibility(View.GONE);
                                itnum.setVisibility(View.GONE);
                                thoughts.setVisibility(View.GONE);
                                feed.setVisibility(View.GONE);
                                send.setVisibility(View.GONE);
                                SharedPreferences.Editor editor = preferences
                                        .edit();
                                editor.putBoolean("tgpref", true);
                                editor.commit();
                            }
                        });
                alertDialog.show();
                // End

            }
        };
    });

    Button statusbutton = (Button) findViewById(R.id.statusbutton);
    statusbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);
            // End

            /** Fading Transition Effect */
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            /** Status Bar */
            final ProgressDialog dialog = new ProgressDialog(screen1.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setMessage("Getting Status Please Wait...");
            dialog.show();

            Thread t = new Thread(new Runnable() {
                public void run() {
                    while (dialog.getProgress() < dialog.getMax()) {
                        try {

                            // Send username and password strings into
                            // screen1 class
                            Intent i = new Intent(screen1.this,
                                    statuspage.class);
                            i.putExtra("uid", username);
                            i.putExtra("pwd", password);
                            startActivity(i);
                            // End

                            Thread.sleep(50);
                        } catch (Exception e) {/* no-op */
                        }
                    }
                    dialog.dismiss();
                }
            });
            t.start();
            /** End Of Status Bar */

        }

    });

    Button jobs = (Button) findViewById(R.id.jobs);
    jobs.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);

            startActivity(new Intent(screen1.this, Jobs.class));
            screen1.this.finish();

            /** Fading Transition Effect */
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            /** Status Bar */
            final ProgressDialog dialog2 = new ProgressDialog(screen1.this);
            dialog2.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog2.setMessage("Loading Jobs Please Wait...");
            dialog2.show();

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    while (dialog2.getProgress() < dialog2.getMax()) {
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {/* no-op */
                        }
                    }
                    dialog2.dismiss();
                }
            });
            t1.start();
            /** End Of Status Bar */
        }

    });

    Button contact = (Button) findViewById(R.id.contact);
    contact.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);

            dispatch.setVisibility(View.VISIBLE);
            dispatchnum.setVisibility(View.VISIBLE);
            csr.setVisibility(View.VISIBLE);
            csrnum.setVisibility(View.VISIBLE);
            it.setVisibility(View.VISIBLE);
            itnum.setVisibility(View.VISIBLE);
            milein.setVisibility(View.GONE);
            zipin.setVisibility(View.GONE);
            mileout.setVisibility(View.GONE);
            zipout.setVisibility(View.GONE);
            submit.setVisibility(View.GONE);
            thoughts.setVisibility(View.GONE);
            feed.setVisibility(View.GONE);
            send.setVisibility(View.GONE);

        }
    });

    Button feedback = (Button) findViewById(R.id.feedback);
    feedback.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            // Button OnClick Vibrating Service
            Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vib.vibrate(50);

            thoughts.setVisibility(View.VISIBLE);
            feed.setVisibility(View.VISIBLE);
            send.setVisibility(View.VISIBLE);
            milein.setVisibility(View.GONE);
            zipin.setVisibility(View.GONE);
            mileout.setVisibility(View.GONE);
            zipout.setVisibility(View.GONE);
            submit.setVisibility(View.GONE);
            dispatch.setVisibility(View.GONE);
            dispatchnum.setVisibility(View.GONE);
            csr.setVisibility(View.GONE);
            csrnum.setVisibility(View.GONE);
            it.setVisibility(View.GONE);
            itnum.setVisibility(View.GONE);
        }
    });
}

@Override
public void onBackPressed() {

    // Button OnClick Vibrating Service
    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vib.vibrate(50);

    startActivity(new Intent(screen1.this, AgentPortalActivity.class));
    screen1.this.finish();

    /** Fading Transition Effect */
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    return;

}

@Override
public void onClick(View v) {

}

}

这是我的LogCat

03-22 09:44:56.031: I/Process(21770): Sending signal. PID: 21770 SIG: 9
03-22 09:44:57.679: D/dalvikvm(21798): GC_EXTERNAL_ALLOC freed 58K, 44% free 3036K/5379K, external 1596K/2108K, paused 35ms
03-22 09:44:57.788: D/dalvikvm(21798): GC_EXTERNAL_ALLOC freed 11K, 44% free 3056K/5379K, external 3498K/3748K, paused 36ms
03-22 09:44:57.804: D/AndroidRuntime(21798): Shutting down VM
03-22 09:44:57.804: W/dalvikvm(21798): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-22 09:44:57.812: E/AndroidRuntime(21798): FATAL EXCEPTION: main
03-22 09:44:57.812: E/AndroidRuntime(21798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jetdelivery.mobile/com.jetdelivery.mobile.AgentPortalActivity}: java.lang.NullPointerException
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.os.Looper.loop(Looper.java:130)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread.main(ActivityThread.java:3687)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at java.lang.reflect.Method.invoke(Method.java:507)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at dalvik.system.NativeStart.main(Native Method)
03-22 09:44:57.812: E/AndroidRuntime(21798): Caused by: java.lang.NullPointerException
03-22 09:44:57.812: E/AndroidRuntime(21798):    at com.jetdelivery.mobile.AgentPortalActivity.onCreate(AgentPortalActivity.java:40)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 09:44:57.812: E/AndroidRuntime(21798):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-22 09:44:57.812: E/AndroidRuntime(21798):    ... 11 more
03-22 09:45:04.652: I/Process(21798): Sending signal. PID: 21798 SIG: 9

1 个答案:

答案 0 :(得分:0)

// Converts the Text in the User Id and Password field to Strings
    username = uid.getText().toString();
    password = pwd.getText().toString();

These lines正在为您提供NullPointerExcepton。因为当它们不包含任何东西时,你试图从这些edittexts中获取OnCreate()本身的文本。在onResume()之前,用户无法查看和交互视图。您需要在onCreate和某些事件触发(例如按钮单击)上关闭这两行和所有其他代码。 试试这个代码。我已将它们移动到onClick()以给你一个想法:

public class AgentPortalActivity extends Activity implements OnClickListener {

String username;
String password;
Button login;
EditText uid;
EditText pwd;
CheckBox chkRememberPassword;
SharedPreferences loginPreferences;
SharedPreferences.Editor loginPrefsEditor;
Boolean saveLogin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // User Id and Password Text Fields
    uid = (EditText) findViewById(R.id.uid);
    pwd = (EditText) findViewById(R.id.pw);



    // Login Button On Click Listener
    login = (Button) findViewById(R.id.login);
    login.setOnClickListener(this);

    // Remember Password Save Properties
    chkRememberPassword = (CheckBox) findViewById(R.id.chkRememberPassword);
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
    loginPrefsEditor = loginPreferences.edit();

    saveLogin = loginPreferences.getBoolean("saveLogin", false);

    if (saveLogin == true) {
        uid.setText(loginPreferences.getString(username, ""));
        pwd.setText(loginPreferences.getString(password, ""));
        chkRememberPassword.setChecked(true);
        // End

    }
}

public void onClick(View view) {
    if (view == login) {


         // Converts the Text in the User Id and Password field to Strings
        username = uid.getText().toString();
        password = pwd.getText().toString();

        // Loading Dialog Box
        final ProgressDialog dialog = new ProgressDialog(
                AgentPortalActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Logging In...");
        dialog.show();

        Thread t = new Thread(new Runnable() {
            public void run() {
                while (dialog.getProgress() < dialog.getMax()) {

                    try {

                        Thread.sleep(50);
                    } catch (Exception e) {/* no-op */
                    }
                }
                dialog.dismiss();
            }
        });
        t.start();
        // End Of Loading Dialog Box

        // Hides Soft Keyboard
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(uid.getWindowToken(), 0);
        imm.hideSoftInputFromWindow(pwd.getWindowToken(), 0);

        // Button OnClick Vibrating Service
        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(50);

        // Send username and password strings into screen1 class
        Intent i = new Intent(this, screen1.class);
        i.putExtra("uid", username);
        i.putExtra("pwd", password);
        startActivity(i);
        // End

        if (chkRememberPassword.isChecked()) {
            loginPrefsEditor.putBoolean("saveLogin", true);
            loginPrefsEditor.putString(username, username);
            loginPrefsEditor.putString(password, password);
            loginPrefsEditor.commit();
        } else {
            loginPrefsEditor.clear();
            loginPrefsEditor.commit();
        }

        doSomethingElse();
    }
}

public void doSomethingElse() {

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);

}