简体   繁体   English

运行应用程序时出现java.lang.NullPointerException

[英]java.lang.NullPointerException while running an application

I am new to android development. 我是android开发的新手。 I downloaded source file from the following link 我从以下链接下载了源文件

" http://android-er.blogspot.in/2012/07/implement-custom-linearlayout-for.html " , but while trying to run in emulator it shows http://android-er.blogspot.in/2012/07/implement-custom-linearlayout-for.html ,但是尝试在模拟器中运行时会显示

java.lang.NullPointerException at com.example.androidhorizontalscrollviewgallery.MainActivity.onCreate(MainActivity.java:27)

My MainActivity.java code is below: 我的MainActivity.java代码如下:

package com.example.androidhorizontalscrollviewgallery;

import java.io.File;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

MyHorizontalLayout myHorizontalLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myHorizontalLayout = (MyHorizontalLayout)findViewById(R.id.mygallery);


    File targetDir=getDir("Pictures",Context.MODE_PRIVATE);

    String targetPath=targetDir+ "/homepage/";
    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();

    for(File f : files){

        myHorizontalLayout.add(f.getAbsolutePath());
    }


    }
}

Here I have stored my images in my computer in "Libraries\\Pictures\\homepage" path. 在这里,我将图像存储在计算机中的“ Libraries \\ Pictures \\ homepage”路径中。 I wanted to get the images in gallery but I could not. 我想将图像保存在图库中,但我不能。 I don know what causes this exception can anyone please tell the solution 我不知道是什么原因导致此异常,任何人都可以告诉解决方案

try this.. 尝试这个..

File targetDir=getDir("Libraries\Pictures\homepage",Context.MODE_PRIVATE);
 String targetPath=targetDir.toString();

From the exception, you have a null pointer on line 27, which means that the files object that you are iterating is null. 根据例外,您在第27行有一个空指针,这意味着您要迭代的文件对象为空。 You use the listFiles method to obtain that object, but the listFiles method can return null. 您可以使用listFiles方法获取该对象,但是listFiles方法可以返回null。 You have to check whether you have null or not to avoid your app crashing. 您必须检查是否为null以避免应用崩溃。 See example below. 请参见下面的示例。 Also, refer to the File class documentation . 另外,请参考File类文档

if (files == null) {
    // handle case where the file object is not a directory
}
else {
for(File f : files){
        myHorizontalLayout.add(f.getAbsolutePath());
    }
}

I think you can't directly get images from your computer path . 我认为您不能直接从计算机路径中获取图像

You have to copy that images in your drawable or assests folder of the application. 您必须将图像复制到应用程序的可绘制或资产文件夹中。

Or your images should reside in either your device or emulator gallery or any other folders . 或者,图像应位于设备或仿真器库中任何其他文件夹中 But it should be in device or emulator in whichever you are testing. 但无论您要进行测试,它都应该位于设备或仿真器中。

See this line in tutorial you used. 请参阅您使用的教程中的这一行。

String ExternalStorageDirectoryPath = Environment
      .getExternalStorageDirectory()
      .getAbsolutePath();

String targetPath = ExternalStorageDirectoryPath + "/test/";

So this is your device's sdcard path - external storage path. 这就是您设备的sdcard路径 -外部存储路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 Kafka 运行 Springboot 应用程序时出现 java.lang.NullPointerException - java.lang.NullPointerException while running Springboot application for Kafka 运行我的小程序时出现 java.lang.NullPointerException - java.lang.NullPointerException while running my applet 运行mahout时线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException while running mahout 运行Cucumber测试时出现java.lang.NullPointerException - java.lang.NullPointerException while running a Cucumber test 执行多线程数据库应用程序时出现java.lang.NullPointerException - java.lang.NullPointerException while executing multithreaded database application java.lang.NullPointerException:使用Firebase时 - java.lang.NullPointerException: while using firebase 设置时间时java.lang.NullPointerException - java.lang.NullPointerException while setting time 创建DiskFileItem时java.lang.NullPointerException - java.lang.NullPointerException while creating DiskFileItem 使用ServletContext时java.lang.NullPointerException - java.lang.NullPointerException while using ServletContext LWJGL应用程序” java.lang.NullPointerException - LWJGL Application" java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM