2017-04-16 78 views
0

請幫助我是新手,我得到我得到這個錯誤? 當我啓動應用程序,我請求存儲權限,但在此之後,應用程序崩潰,並給我這個錯誤: - 嘗試獲取空數組的長度。請幫忙。嘗試獲得file.Length的null數組的長度()

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techx.storysaver/com.techx.storysaver.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6126) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
    Caused by: java.lang.NullPointerException: Attempt to get length of null array 
     at com.techx.storysaver.ImageAdapter.getCount(ImageAdapter.java:40) 
     at android.widget.GridView.setAdapter(GridView.java:206) 
     at com.techx.storysaver.ImageFragment.onCreateView(ImageFragment.java:53) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757) 
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355) 
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146) 
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008) 
     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
     at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249) 
     at android.app.Activity.performStart(Activity.java:6696) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
     at android.app.ActivityThread.-wrap12(ActivityThread.java)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:154)  
     at android.app.ActivityThread.main(ActivityThread.java:6126)  
     at java.lang.reflect.Method.invoke(Native Method) 

這是我ImageAdapter

public class ImageAdapter extends BaseAdapter { 

    private Context context; 


    String path = Environment.getExternalStorageDirectory().toString()+"/Pictures/"; 
    File f = new File(path); 
    File file[] = f.listFiles(); 



    public ImageAdapter(Context c) 
    { 
     context = c; 
    } 


    @Override 
    public int getCount() { 

      return file.length;  //Line 40, Here i am gettin Error 

    } 

    @Override 
    public Object getItem(int position) { 
     return file[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(context); 
     Glide 
       .with(context) 
       .load(file[position]) 
       .into(imageView); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(new GridView.LayoutParams(360,480)); 

     return imageView; 
    } 

} 

這裏是我的圖像碎片

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

    GridView gridView = (GridView)rootView.findViewById(R.id.grid_view); 
    gridView.setAdapter(new ImageAdapter(getActivity())); //Line 50, Here Also i am getting Error 



    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent i = new Intent(getActivity(),FullImageActivity.class); 
      i.putExtra("id",position); 
      startActivity(i); 
     } 
    }); 

    return rootView; 
} 
+0

的【什麼是一個NullPointerException,如何解決呢?(可能的複製http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception - 和 - 如何 - 我 - 修復它) – csmckelvey

+0

@Bubletan,你是如何對齊這樣的代碼,你用什麼按鍵? – Remario

+0

@Remario你的意思是?我使用了編輯器。 – Bubletan

回答

2
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures/"; 
File f = new File(path); 
File file[] = f.listFiles(); 

file[]爲空,所以這種說法return file.length;或任何其他涉及該變量將失敗。也可以使用異常處理來處理文件存在。例如像FileNotFound。

也可以嘗試這個String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

String pic_path = String.format("%s/%s",path,specific_path); 

使用此而不是得到一個處理文件。

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 

然後if(storageDir.exists()){File file[] = storageDir.listFiles();}

語法調用getExternalFilesDir,這取決於上下文。 。在片段

  • context.getExternalFilesDir()中的類,在這裏通過語境 作爲參數
  • YourActivity.this.getExternalFilesDir

    1. getActivity()getExternalFilesDir()();當 在內部類活動的所謂
  • +0

    評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/141904/discussion-on-answer-by-remario-attempt-to-get-length-of-null-array-of-文件楞)。 –

    相關問題