无法弄清楚使用Fragments的致命异常

时间:2014-05-25 15:49:18

标签: android fragment alertdialog

在这里粘贴一些代码,我需要帮助找出错误。一切正常,直到我开始搞乱创建AlertDialogFragment,现在RegisterFragment抛出异常。

logcat的:

   05-25 11:36:25.168: D/LoginActivity(4173): showRegister() invoked
   05-25 11:36:25.168: D/LoginActivity(4173): bundle put
   05-25 11:36:25.238: D/AndroidRuntime(4173): Shutting down VM
   05-25 11:36:25.248: W/dalvikvm(4173): threadid=1: thread exiting with uncaught exception (group=0xb3a95ba8)
   05-25 11:36:25.268: E/AndroidRuntime(4173): FATAL EXCEPTION: main
   05-25 11:36:25.268: E/AndroidRuntime(4173): Process: com.lamesauce.atplogbook, PID: 4173
   05-25 11:36:25.268: E/AndroidRuntime(4173): java.lang.IllegalStateException: Could not execute method of the activity
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.view.View$1.onClick(View.java:3823)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.view.View.performClick(View.java:4438)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.view.View$PerformClick.run(View.java:18422)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.os.Handler.handleCallback(Handler.java:733)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.os.Handler.dispatchMessage(Handler.java:95)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.os.Looper.loop(Looper.java:136)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.app.ActivityThread.main(ActivityThread.java:5017)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at java.lang.reflect.Method.invokeNative(Native Method)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at java.lang.reflect.Method.invoke(Method.java:515)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at dalvik.system.NativeStart.main(Native Method)
   05-25 11:36:25.268: E/AndroidRuntime(4173): Caused by: java.lang.reflect.InvocationTargetException
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at java.lang.reflect.Method.invokeNative(Native Method)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at java.lang.reflect.Method.invoke(Method.java:515)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at android.view.View$1.onClick(View.java:3818)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  ... 11 more
   05-25 11:36:25.268: E/AndroidRuntime(4173): Caused by: java.lang.NullPointerException
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at com.lamesauce.atplogbook.RegistrationFragment.<init>(RegistrationFragment.java:19)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  at com.lamesauce.atplogbook.LoginActivity.showRegister(LoginActivity.java:102)
   05-25 11:36:25.268: E/AndroidRuntime(4173):  ... 14 more

这是调用方法:

public void showRegister(View view) {
    Log.d(TAG, "showRegister() invoked");
    EditText emailText = (EditText) findViewById(R.id.edittextEmail);
    String registerEmail = emailText.getText().toString();
    Bundle newBundle = new Bundle();
    newBundle.putString("registerEmail", registerEmail);
    Log.d(TAG, "bundle put");
    RegistrationFragment newFragment = new RegistrationFragment();
    Log.d(TAG, "new fragment created");
    newFragment.setArguments(newBundle);
    Log.d(TAG, "setArguments()");
     ...
     }

应该加载的片段:

 package com.lamesauce.atplogbook;

 import android.app.ProgressDialog;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;

 public class RegistrationFragment extends Fragment {

private static final String TAG = RegistrationFragment.class
        .getSimpleName();
private Utils utils = new Utils();
final User user = (User) this.getActivity().getApplicationContext();
private String registrationEmail;
private String password1;
private String password2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_register, container,
            false);

    final EditText registerEmail = (EditText) view
            .findViewById(R.id.edittextRegisterEmail);
    final EditText registerPassword = (EditText) view
            .findViewById(R.id.edittextRegisterPassword);
    final EditText registerPassword2 = (EditText) view
            .findViewById(R.id.edittextPasswordAgain);

    // set up button onClickListener
    Button registerButton = (Button) view
            .findViewById(R.id.buttonTryRegister);
    registerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Attempt to create a new account on Parse.com

            Log.d(TAG, "trying to register");

            // EditText emailText = (EditText)
            // view.findViewById(R.id.edittextRegisterEmail);
            registrationEmail = (String) registerEmail.getText().toString();

            if (utils.isValidEmail(registrationEmail)) {
                Log.d(TAG, "good email");
            } else {
                Log.d(TAG, "bad email");
                Toast.makeText(getActivity(), "invalid email address",
                        Toast.LENGTH_SHORT).show();

                registerEmail.selectAll();
                registerEmail.setFocusableInTouchMode(true);
                registerEmail.requestFocus();

                return;
            }

            // do passwords match?
            password1 = (String) registerPassword.getText().toString();
            password2 = (String) registerPassword2.getText().toString();

            if (password1.equals(password2)) {
                Log.d(TAG, "passwords match, continuing");
            } else {
                Log.d(TAG, "passwords do not match");
                Toast.makeText(getActivity(), "passwords do not match",
                        Toast.LENGTH_SHORT).show();

                // Give the password field the focus
                registerPassword.setFocusableInTouchMode(true);
                registerPassword.requestFocus();
                return;
            }

            if (password1.length() < 6) {
                Log.d(TAG, "Password not 6 characters");
                Toast.makeText(getActivity(),
                        "password must be 6 characters minimum",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            // TODO
            // try to register

            boolean success = false;
            boolean retry = true;
            int attempts = 0;

            do {
                attempts++;
                Log.d(TAG, "send attempt: " + Float.toString(attempts));
                success = sendRegistration();
                // retry = utils.showConfirmationBox(
                // "try registration again?", getActivity());
                retry = false;
            } while (retry);
            // success result

            // fail result
            // try again

            // yes
            // return to thread try
            // no
            // close window

        }
    });

    // finish up setting up the view
    Bundle b = getArguments();
    registerEmail.setText(b.getString("registerEmail"));

    return view;

}

private boolean sendRegistration() {
    boolean success = false;
    final ProgressDialog myPd_ring = ProgressDialog.show(getActivity(),
            "Trying to register", "Contacting server...");
    myPd_ring.setCancelable(true);
    new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                Log.d(TAG, "running progress");
                user.setLoginEmail(registrationEmail);
                user.setLoginPassword(password1);

                Thread.sleep(5000);
                // delete this

            } catch (Exception e) {
            }
            myPd_ring.dismiss();
        }
    }).start();
    return success;
}

 }

0 个答案:

没有答案