Android库中的getAssets()方法不起作用

Sae*_*omi 6 java android android-assets

作为 Android 编程的新手,我已将四个文件附加到我的库的资产文件夹中。
您可以看到附图。

图书馆

我想使用下面的代码通过 Android 库访问这些文件(如果我不在 Android 库中使用此代码,则当我在 MainActivity 中使用它时,它运行时不会出现错误)。

public void copy_weights() {
    try {
        AssetManager assetFiles = getAssets();
        String[] files = assetFiles.list("");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();  //catch the error in this line
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

但这段代码给了我错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.Context.getAssets' on a null object refrence

我尝试了其他方法,例如直接访问home:///android_asset/7.cfg,但也无法访问文件。

编辑1

我像下面这样copy_weights()调用mainActivity

public class MainActivity extends AppCompatActivity {
@Override

protected void onCreate(Bundle savedInstanceState) {
    final new_class newClass=new new_class();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button =(Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView textView=(TextView) findViewById(R.id.textView);
            newClass.copy_weights();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

编辑2
new_class代码

package com.example.mylittlelibrary;
import android.content.res.AssetManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity; 
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;

public class new_class extends AppCompatActivity {

    private boolean opencv_sucessfully_included;
    private static int BUFFER_SIZE = 1024;

    public new_class() { }

    private static void copyAssetFiles(InputStream in, OutputStream out) {
        try {

            byte[] buffer = new byte[BUFFER_SIZE];
            int read;

            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void copy_weights() {
        try {
            AssetManager assetFiles = getAssets();
            // MyHtmlFiles is the name of folder from inside our assets folder
            String[] files = assetFiles.list("");
            // Initialize streams
            InputStream in = null;
            OutputStream out = null;
            for (int i = 0; i < files.length; i++) {
                File file = new File(Environment.getExternalStorageDirectory() + "/" + files[i]);
                if (!file.exists()) {
                    in = assetFiles.open(files[i]);
                    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                    copyAssetFiles(in, out);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

The*_*rer 7

您不能像这样初始化 Activity。如果您需要对 Context 的引用(例如 with getAssets()),您应该将其传递给类。

首先,删除您的AppCompatActivity扩展:

public class new_class {
Run Code Online (Sandbox Code Playgroud)

然后更改构造函数并添加全局变量:

private Context context;

public new_class(Context context) {
    this.context = context;
}
Run Code Online (Sandbox Code Playgroud)

您会注意到您的一些方法现在呈红色且未解决。context.只需在它们之前添加即可。例如

context.getAssets()
Run Code Online (Sandbox Code Playgroud)

通过向该类添加参数来创建该类的新实例this

final new_class newClass = new new_class(this);
Run Code Online (Sandbox Code Playgroud)

另外,您不应该在super.onCreate()调用之前放置任何内容,因此请将其移至调用之后。