gpt4 book ai didi

java - 如何获取并显示数组的长度?

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:42 24 4
gpt4 key购买 nike

我想为我的应用程序创建一个文件夹。为此,需要在运行时检查权限。因此,当用户接受请求时,我将创建文件夹并使用目录中的文件数初始化 listFile 数组。

我还有一个 asyncTask 来获取事件。因此,我在 asyncTask 的 onPostExecute 方法中创建主列表。

在此主列表中,我想显示 listFile 数组的长度。但它因空指针异常而崩溃。

主要 Activity :

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,GetEventsAsyncTask.GetEvents {

private RecyclerView recyclerView;
private MainListAdapter mAdapter;
private List<MainList> mainLists = new ArrayList<>();


String folder_main = "MeaVita";
File dir = new File(Environment.getExternalStorageDirectory(),folder_main);

File[] listFile;
public int REQUEST_CODE_ASK_PERMISSIONS = 11;
public boolean permissionsAccepted;
private String userUsername;

public int PERMISSIONS_WRITE_EXTERNAL_STORAGE =10;


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

setContentView(R.layout.activity_main);

sharedpreferences = getSharedPreferences("username",Context.MODE_PRIVATE);

userUsername = sharedpreferences.getString("UserUsername","No name defined");

GetEventsAsyncTask getEventsAsyncTask = new GetEventsAsyncTask(this,MainActivity.this,eventArrayList);
getEventsAsyncTask.execute(userUsername);

parent = (AppBarLayout) findViewById(R.id.view3);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

eventArrayList = new ArrayList<>();

checkFilePermissions();

recyclerView.setAdapter(new MainListAdapter(mainLists, new MainListAdapter.OnItemClickListener() {
@Override public void onItemClick(MainList item) {

if(item.getTitle().equals("Plan Event")) {
startActivity(new Intent(MainActivity.this, PlanEventActivity.class));
}
if(item.getTitle().equals("Events")) {
startActivity(new Intent(MainActivity.this, EventsListActivity.class));
}
if(item.getTitle().equals("Photos")) {
// checkFilePermissions();

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

permissionsAccepted = sharedpreferences.getBoolean("permission",false);

if(dir.exists()) {

if (permissionsAccepted) {


if (listFile.length == 0) {


showAlert("No Images");

} else {

new MainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
}

}
}
else {

showAlert("No Folder");
}

}
if(item.getTitle().equals("Reminders")) {
startActivity(new Intent(MainActivity.this, RemindersActivity.class));
}
if(item.getTitle().equals("Chat")) {

}
if(item.getTitle().equals("Notes")) {
startActivity(new Intent(MainActivity.this, NotesActivity.class));
}
if(item.getTitle().equals("Notifications")) {
startActivity(new Intent(MainActivity.this, NotificationsActivity.class));
}
if(item.getTitle().equals("Profile")) {
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
}
}
}));

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());


}

public class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingleMediaScanner(Context context, File f) {
mFile = f;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}

public void onMediaScannerConnected() {
mMs.scanFile(mFile.getAbsolutePath(), null);
}

public void onScanCompleted(String path, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
mMs.disconnect();
}

}

private void checkFilePermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
}

}
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (REQUEST_CODE_ASK_PERMISSIONS == 11) {

// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {


if (!dir.exists()) {
dir.mkdirs();
permissionsAccepted = true;
}

listFile = dir.listFiles();


SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("permission",permissionsAccepted);
editor.apply();


if (listFile.length == 0) {
Toast.makeText(MainActivity.this, "The folder is empty.", Toast.LENGTH_SHORT).show();
}

else {

new MainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
}
// permission was granted, yay! Do the
// contacts-related task you need to do.

} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Request Denied", Toast.LENGTH_SHORT).show();

}


// other 'case' lines to check for other
// permissions this app might request
}
}

private void prepareData() {



MainList mainList = new MainList("Plan Event","", R.drawable.plan);
mainLists.add(mainList);

mainList = new MainList("Events",String.valueOf(eventArrayList.size()), R.drawable.events);
mainLists.add(mainList);

mainList = new MainList("Photos",String.valueOf(listFile.length), R.drawable.phone);
mainLists.add(mainList);

mainList = new MainList("Reminders","20", R.drawable.remind);
mainLists.add(mainList);

mainList = new MainList("Chat","2", R.drawable.chat);
mainLists.add(mainList);

mainList = new MainList("Notes","2", R.drawable.notes);
mainLists.add(mainList);

mainList = new MainList("Notifications","20", R.drawable.remind);
mainLists.add(mainList);

mainList = new MainList("Profile","50%", R.drawable.user);
mainLists.add(mainList);

}
public void showAlert(String alert) {

Snackbar snackbar = Snackbar.make(parent, alert, Snackbar.LENGTH_LONG);
snackbar.show();
}
@Override
public void doPostExecute(ArrayList<Event> response) {

eventArrayList = response;
listFile = dir.listFiles();
prepareData();

}
}

日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.siddhi.meavita, PID: 30194
java.lang.NullPointerException: Attempt to get length of null array
at com.example.siddhi.meavita.Activities.MainActivity.prepareData(MainActivity.java:341)
at com.example.siddhi.meavita.Activities.MainActivity.doPostExecute(MainActivity.java:370)
at com.example.siddhi.meavita.AsyncTaskClasses.GetEventsAsyncTask.onPostExecute(GetEventsAsyncTask.java:107)
at com.example.siddhi.meavita.AsyncTaskClasses.GetEventsAsyncTask.onPostExecute(GetEventsAsyncTask.java:28)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)

如何以及何时初始化listFile数组以及何时调用prepareData方法?

请帮忙。坚持这一点,对此非常新鲜。谢谢。

最佳答案

您正在使用方法File.listFiles()从目录中获取文件列表。

listFile 可以为 null

但是,根据Docs ,该方法可以返回null

public File[] listFiles()
...
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

dir.listFile() 在以下情况下可能会返回 null:

  1. 如果 dir 不反射(reflect)目录(dir 可以指向一个文件,例如......这不是你的情况)
  2. 任何 I/O 错误(例如读取目录内容期间的权限或任何其他故障。

如您所见,您不能确保 listFile 始终不同于 null`...它偶尔可以为 null。因此,在读取其属性/值之前,您应该首先检查 listFile 是否为 null。

空保护

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
listFile = dir.listFiles();
...
if (listFile == null || listFile.length == 0) {
Toast.makeText(MainActivity.this, "The folder is empty.", Toast.LENGTH_SHORT).show();
} else {
new MainActivity.SingleMediaScanner(MainActivity.this, listFile[0]);
}
...
}

并且

private void prepareData() {
...
if(listFile != null && listFile.length >= 0)
mainList = new MainList("Photos",String.valueOf(listFile.length), R.drawable.phone);
else
mainList = new MainList("Photos", "0", R.drawable.phone);

mainLists.add(mainList);
...
}

关于java - 如何获取并显示数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106294/

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