草庐IT

java - File.delete() 被忽略

coder 2023-12-16 原文

<分区>

我目前遇到一些错误,它给了我空指针。所以这是代码。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.catalog_view);
    name = (TextView) findViewById(R.id.textView1);
    buttonDel = (Button) findViewById(R.id.buttonDel);
    buttonBack = (ImageButton) findViewById(R.id.imageButton1);


    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

    Bundle bundle = getIntent().getExtras();
    mPath = bundle.getString("path");

    thelabels = new labels(mPath);
    thelabels.Read();

    count = 0;
    int max = thelabels.max();
    for (int i = 0; i <= max; i++)
    {
        if (thelabels.get(i) != "") {
            count++;
        }
    }

    bmlist = new Bitmap[count];
    namelist = new String[count];
    count = 0;
    for (int i = 0; i < max; i++){
        if (thelabels.get(i) != "");{
            File root = new File(mPath);
            final String fname = thelabels.get(i);
            FilenameFilter pngFilter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().startsWith(fname.toLowerCase() + "-");
                }
            };

            File[] imageFiles = root.listFiles(pngFilter);
            if (imageFiles.length > 0){
                InputStream is;
                try{
                    is = new FileInputStream(imageFiles[0]);

                    bmlist[count] = BitmapFactory.decodeStream(is);
                    namelist[count] = thelabels.get(i);
                }
                catch (FileNotFoundException e){
                    // TODO Auto-generated catch block
                    Log.e("File error", e.getMessage() + " " + e.getCause());
                    e.printStackTrace();
                }
            }

            count++;
        }
    }
    g = (Gallery) findViewById(R.id.gallery1);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);


    buttonBack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });

    buttonDel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            File root = new File(mPath);
            FilenameFilter pngFilter = new FilenameFilter() {
                public boolean accept(File dir, String n) {
                    String s = name.getText().toString();
                    return n.toLowerCase().startsWith(s.toLowerCase() + "-");
                }
            };
            File[] imageFiles = root.listFiles(pngFilter);
            for (File image : imageFiles) {
                image.delete();
                int i;
                for (i = 0; i < count; i++) {
                    if (namelist[i].equalsIgnoreCase(name.getText().toString())) {
                        Log.i("Delete operation", name.getText().toString());
                        int j;
                        for (j = i; j < count - 1; j++) {
                            namelist[j] = namelist[j + 1];
                            bmlist[j] = bmlist[j + 1];
                        }
                        count--;
                        refresh();
                        break;
                    }
                }
            }
        }
    });
}

错误指向这一行

if (namelist[i].equalsIgnoreCase(name.getText().toString()))

错误

 Process: com.example.syafiq.facialrecognition, PID: 29294
                                                                                  java.lang.NullPointerException
                                                                                      at com.example.syafiq.facialrecognition.ImageGallery$3.onClick(ImageGallery.java:135)
                                                                                      at android.view.View.performClick(View.java:4654)
                                                                                      at android.view.View$PerformClick.run(View.java:19438)
                                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:146)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                                      at dalvik.system.NativeStart.main(Native Method)

如果我的问题没有准备好,我真的很抱歉。我是 android 编程的新手。我真的希望你们能帮我解决这个错误 :( 非常感谢你们抽出宝贵的时间。

有关java - File.delete() 被忽略的更多相关文章

  1. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  2. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  3. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  4. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  5. ruby CSV : How can I read a tab-delimited file? - 2

    CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|

  6. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  7. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  8. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

  9. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  10. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

随机推荐