Android - 片段onCreate在应用启动时崩溃

时间:2014-10-01 01:13:39

标签: android android-fragments android-logcat oncreate kiicloud

嗨我正在处理一个片段,该片段有一个按钮,允许用户选择要上传的图片。但是,当试图测试片段时,它会崩溃。它说它崩溃,如果我删除代码然后按钮显示,我可以去我的画廊选择一张图片,但一旦我点击图片,应用程序再次崩溃。

Logcat -

  10-01 01:18:39.618    7982-7982/com.myapp.app.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.myapp.app.myapp.Social.model.UserProfiles.ProfilePic.onCreate(ProfilePic.java:49)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
            at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:838)
            at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:843)
            at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:15291)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4819)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15291)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:861)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
            at android.view.View.measure(View.java:15291)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4819)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
            at android.view.View.measure(View.java:15291)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1867)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1292)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4242)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4941)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
            at dalvik.system.NativeStart.main(Native Method)

这是片段 -

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.kii.cloud.storage.KiiObject;
import com.kii.cloud.storage.resumabletransfer.KiiRTransfer;
import com.kii.cloud.storage.resumabletransfer.KiiRTransferCallback;
import com.kii.cloud.storage.resumabletransfer.KiiUploader;
import com.myapp.app.myapp.R;

import java.io.File;
import java.io.FileOutputStream;

public class ProfilePic extends Fragment {
    private static final String TAG = "ProfilePic";
    String objectUri = null;
    private static final int PICK_IMAGE = 1;
    private Activity activity;
private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_attach_file, container,
                false);
        return view;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        objectUri = getArguments().getString("object_uri");
        Button attachButton = (Button)
                getView().findViewById(R.id.attach_file_button);
        attachButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onAttachFileButtonClicked(v);
            }
        });
        setPageImage(3);
        ImageView imageView = (ImageView) getView().findViewById(R.id.details);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }

    public void onAttachFileButtonClicked(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri selectedFileUri = data.getData();
            String filePath = getFilePathByUri(selectedFileUri);
            Log.v(TAG, "Picture Path : " + filePath);
            if (filePath == null) {
                showAlert("File not exists, Please select an image that exists locally.");
                return;
            }
            uploadFile(filePath);
        } else {
            showToast("picking file failed!");
        }
    }

    private void showToast(String message) {
        Toast.makeText(this.activity, message, Toast.LENGTH_SHORT).show();
    }

    private String getFilePathByUri(Uri selectedFileUri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // Workaround of retrieving file image through ContentResolver
            // for Android4.2 or later
            String filePath = null;
            FileOutputStream fos = null;
            try {
                Bitmap bmp = MediaStore.Images.Media.getBitmap(
                        this.activity.getContentResolver(), selectedFileUri);

                String cacheDir = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + File.separator + "app";
                File createDir = new File(cacheDir);
                if (!createDir.exists()) {
                    createDir.mkdir();
                }
                filePath = cacheDir + File.separator + "upload.jpg";
                File file = new File(filePath);

                fos = new FileOutputStream(file);
                bmp.compress(CompressFormat.JPEG, 95, fos);
                fos.flush();
                fos.getFD().sync();
            } catch (Exception e) {
                filePath = null;
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (Exception e) {
                        // Nothing to do
                    }
                }
            }
            return filePath;
        } else {
            String[] filePathColumn = { MediaStore.MediaColumns.DATA };
            Cursor cursor = this.activity.getContentResolver().query(
                    selectedFileUri, filePathColumn, null, null, null);

            if (cursor == null)
                return null;
            try {
                if (!cursor.moveToFirst())
                    return null;
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                if (columnIndex < 0) {
                    return null;
                }
                String picturePath = cursor.getString(columnIndex);
                return picturePath;
            } finally {
                cursor.close();
            }
        }
    }

    private void uploadFile(String path) {
        KiiObject object = KiiObject.createByUri(Uri.parse(objectUri));
        File f = new File(path);
        Log.v(TAG, "file can read : " + f.canRead());
        KiiUploader uploader = object.uploader(this.activity, f);
        uploader.transferAsync(new KiiRTransferCallback() {

            @Override
            public void onStart(KiiRTransfer operator) {
                setFragmentProgress(View.VISIBLE);
            }

            @Override
            public void onTransferCompleted(KiiRTransfer operator, Exception e) {
                setFragmentProgress(View.INVISIBLE);
                if (e == null) {

                } else {

                }
            }
        });
    }

    public void moveFromDialogFragment(Class<?> clazz) {
        if (clazz != null) {
            Intent i = new Intent(this.activity, clazz);
            startActivity(i);
        }
    }

    void showAlert(String message) {
        DialogFragment newFragment = AlertDialogFragment.newInstance(
                R.string.operation_failed, message, null);
        newFragment.show(getFragmentManager(), "dialog");
    }

    void setFragmentProgress(int v) {
        ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                .findFragmentById(R.id.progressFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setProgressBarVisiblity(v);
        }
    }

    void setPageImage(int page) {
        ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                .findFragmentById(R.id.progressFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setPageImage(page);
        }
    }
}

协助所需的任何其他代码请告知我们。

更新 我尝试了这些建议,但我仍然收到同样的错误。它似乎在 -

失败了
 objectUri = args.getString("object_uri");

更新2 我尝试了建议的代码,但在同一行仍然收到相同的错误。我更新了我的代码和logcat以反映所做的更改。

由于

3 个答案:

答案 0 :(得分:1)

onCreateView()返回的视图设置为片段的根视图。在getView()中使用Fragment类的view方法而不是onActivityCreated()

<强>更新

在Fragment生命周期中,添加Fragment时,将按常规顺序调用回调方法。 onAttach() - &gt; onCreate() - &gt; onCreateView() - &gt; onActivityCreated()。

onCreateView()实例化片段的视图,视图设置为根视图。你的代码是正确的。但是,在onCreateView()之前调用onCreate()。尚未在onCreate()中设置片段的根视图。你不能在onCreate()中使用getView()方法。它将返回null。

我建议您使用onActivityCreated()而不是onCreate()来访问片段的视图。

更改

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

答案 1 :(得分:0)

我找到了

Button attachButton = (Button) view.findViewById(R.id.attach_file_button);

view尚未初始化。我认为findViewById()应放在onCreateView()

答案 2 :(得分:0)

更改

 View view = inflater.inflate(R.layout.fragment_attach_file, container,false);

view = inflater.inflate(R.layout.fragment_attach_file, container,false);

您不需要再次声明它,因为它已在您班级的private View view;声明。当您在方法中再次声明它时,它会将赋值赋予自身,因此您的成员变量保留为null。

Shadowing

<强>更新

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      objectUri = getArguments().getString("object_uri");
}

更新2

你没有为你的片段设置任何参数,它是null。

在此行Button attachButton=(Button)getView().findViewById(R.id.attach_file_button);getView也会null返回onCreate,因为在onCreateView之前调用了{{1}}