android无法启动活动ComponentInfo NULL Point Exception

时间:2015-06-14 22:16:09

标签: java android android-intent android-studio android-camera

我的应用程序在不同的手机上工作很奇怪 - 一旦一切都很好,其他时间它崩溃,当我尝试制作照片并将其保存在我的手机内存中。照片显示在内存中的文件夹中,但是当我们应该回到应用程序时它会被关闭:

06-14 23:58:37.448  12344-12344/com.apps.madzia.photo_application E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.apps.madzia.photo_application, PID: 12344
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.madzia.photo_application/com.apps.madzia.photo_application.activity.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4053)
            at android.app.ActivityThread.access$1000(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
            at android.widget.GridView.setAdapter(GridView.java:187)
            at com.apps.madzia.photo_application.fragments.FragmentGallery.onCreateView(FragmentGallery.java:42)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
            at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
            at android.app.Activity.performStart(Activity.java:5468)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4053)
            at android.app.ActivityThread.access$1000(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

我制作照片的功能代码:

public void make_photo() {

        Calendar c;
        c = Calendar.getInstance();
        imageName = "fotka_" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DAY_OF_MONTH)
                + "_" + c.get(Calendar.HOUR_OF_DAY) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
        File katalog = new File(IMAGE_FILE_PATH);
        File photo = new File(IMAGE_FILE_PATH+imageName + ".jpg");

      if( Environment.getExternalStorageState() == Environment.MEDIA_UNKNOWN || Environment.getExternalStorageState() == Environment.MEDIA_REMOVED || Environment.getExternalStorageState() == Environment.MEDIA_UNMOUNTED) {
            Log.d("ExternalStorage", "cannot find memory SD card");
           File file = new File(getFilesDir(), imageName);
           IMAGE_FILE_PATH = file.getParent()+"/";
           Log.d("IMAGE_FILE_PATH", IMAGE_FILE_PATH);

       }else {

       }

        Log.d("photo", photo.getPath());
           if (!katalog.exists()) {
               katalog.mkdirs();

           }
           try {
//            fotka = File.createNewFile(imageName, ".jpg", katalog);
               photo.createNewFile();
           } catch (IOException e) {
               e.printStackTrace();
           }

            Intent zdjecie = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            zdjecie.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            startActivityForResult(zdjecie, 1);

    }

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.NullPointerException
            at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
            at android.widget.GridView.setAdapter(GridView.java:187)
            at com.apps.madzia.photo_application.fragments.FragmentGallery.onCreateView(FragmentGallery.java:42)

这意味着在第42行的onCreateView方法的FragmentGallery中你设置了null ArrayAdapter