首先,我是Android编程和android studio
的新手。我有一个库,已将4个文件附加到库的assets folder
中,您可以在下图中看到
我想通过下面的代码访问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();
}
}
但是此代码给我一个错误: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();
}
});
}
编辑2 新类代码
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();
}
}
答案 0 :(得分:1)
您不能像这样初始化一个活动。如果需要引用上下文(例如getAssets()
),则应将其传递给类。
首先,删除您的AppCompatActivity
扩展名:
public class new_class {
然后更改构造函数并添加一个全局变量:
private Context context;
public new_class(Context context) {
this.context = context;
}
您会发现您的某些方法现在显示为红色且尚未解决。只需在它们前面添加context.
。例如
context.getAssets()
通过添加一个this
自变量来创建该类的新实例:
final new_class newClass = new new_class(this);
此外,在调用super.onCreate()
之前不要放置任何内容,因此请将其移到后面。