gpt4 book ai didi

java - 尝试获取 file.Length() 的 null 数组的长度

转载 作者:行者123 更新时间:2023-12-02 02:53:30 26 4
gpt4 key购买 nike

请帮助我是新手,我收到此错误?当我启动应用程序时,我请求存储权限,但之后应用程序崩溃并给出此错误:- 尝试获取空数组的长度。请帮忙。

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;
}

}

这是我的图像 fragment

@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;
}

最佳答案

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

file[] 为 null,因此此语句 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 的语法,取决于上下文。

  1. Fragment 中的 getActivity().getExternalFilesDir()
  2. 类中的 context.getExternalFilesDir() ,您可以在其中传递 Context 作为参数
  3. YourActivity.this.getExternalFilesDir();什么时候 在Activity的内部类中调用

关于java - 尝试获取 file.Length() 的 null 数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43437279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com