請幫助我是新手,我得到我得到這個錯誤? 當我啓動應用程序,我請求存儲權限,但在此之後,應用程序崩潰,並給我這個錯誤: - 嘗試獲取空數組的長度。請幫忙。嘗試獲得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;
}
的【什麼是一個NullPointerException,如何解決呢?(可能的複製http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception - 和 - 如何 - 我 - 修復它) – csmckelvey
@Bubletan,你是如何對齊這樣的代碼,你用什麼按鍵? – Remario
@Remario你的意思是?我使用了編輯器。 – Bubletan