Android旋转崩溃图像

时间:2016-05-03 08:50:28

标签: android

当我旋转我的应用时,它会崩溃:

05-03 10:42:45.455 30008-30052/nl.company.employeedirectory E/static_methods: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
                                                                                       at nl.company.employeedirectory.static_methods.downloadProfilePicture(static_methods.java:88)
                                                                                       at nl.company.employeedirectory.fragments.profile$3.run(profile.java:158)
                                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                       at java.lang.Thread.run(Thread.java:818)
05-03 10:42:45.455 30008-30052/nl.company.employeedirectory E/profile: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
                                                                                at nl.company.employeedirectory.fragments.profile$3.run(profile.java:162)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                at java.lang.Thread.run(Thread.java:818)
05-03 10:42:45.455 30008-30052/nl.company.employeedirectory E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                   Process: nl.company.employeedirectory, PID: 30008
                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
                                                                                       at nl.company.employeedirectory.fragments.profile$3.run(profile.java:195)
                                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                       at java.lang.Thread.run(Thread.java:818)

看起来我的downloadProfilePicture方法有一些东西:

/**
 * ProfielFoto employee downloaden
 *
 * @param employee
 * @param context
 * @throws Exception
 */
public static void downloadProfilePicture(Employee employee, Context context)
{
    try
    {
        InputStream input       = null;
        FileOutputStream output = null;

        URL url = new URL(static_urls.Employee.profilePicture(employee.getEmployeeId(), employee.getProfilePictureString()));

        String outputName = employee.getProfilePictureString();

        input   = url.openConnection().getInputStream();
        output  = context.openFileOutput(outputName, Context.MODE_PRIVATE);

        int read;
        byte[] data = new byte[1024];
        while ((read = input.read(data)) != -1)
        {
            output.write(data, 0, read);
        }

        if (output != null)
        {
            output.close();
        }

        if (input != null)
        {
            input.close();
        }
    }
    catch(Exception ex)
    {
        Log.e("static_methods", Log.getStackTraceString(ex));
    }
}

这里可能有什么问题?

- 编辑 -

  private void loadProfile(final Employee currentEmployee)
    {
        pullDown.setRefreshing(true);

        AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    employee = new GetEmployee().execute(currentEmployee, currentEmployee.getEmployeeId()).get();
                    employee.setApiToken(currentEmployee.getApiToken());

                    static_methods.downloadProfilePicture(employee, getContext());

                    if(employee != null)
                    {
                        getActivity().runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                profilePicture.setImageBitmap(employee.getProfilePicture(getContext()));
                                firstName.setText(employee.getFirstName());
                                lastName.setText(employee.getLastName());
                                email.setText(employee.getEmail());
                                address.setText(employee.getAddress());
                                zip.setText(employee.getZip());
                                city.setText(employee.getCity());
                                birthDate.setText(String.valueOf(employee.getBirthDate()));
                                bio.setText(employee.getBio());

                                if (employee.getIntern() != null)
                                {
                                    internClass.setVisibility(View.VISIBLE);
                                    internClass.setText(employee.getIntern().getInternClass());

                                    assignment.setVisibility(View.VISIBLE);
                                    assignment.setText(employee.getIntern().getAssignment());
                                }
                            }
                        });
                    }
                }
                catch(Exception ex)
                {
                    Log.e("profile", Log.getStackTraceString(ex));
                }
                finally
                {
                    getActivity().runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            pullDown.setRefreshing(false);

                            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                            inputManager.hideSoftInputFromWindow(firstName.getWindowToken(), 0);
                        }
                    });
                }
            }
        });
    }

0 个答案:

没有答案