我正在尝试在我的活动中创建一个 InputStream。它创建得很好,但我试图将创建的 InputStream 传递给我真正想要使用 InputStream 的类。InputStream 在我的活动的 onClick 方法中创建。第二次我尝试将 InputStream 对象传递给我的“工作类”中的一个方法,我得到一个 NullPointerException。我不想在我的活动中做这项工作。如何传递 InputStream 甚至 AssetManager 对象来使用它。我的“工人阶级”没有扩展或实施任何东西。应该是?
public class MainActivity extends ListActivity {
private DbManagement mdbManager;
private TestProcessor tp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lookup);
mdbManager = new DbManagement(this);
mdbManager.open();
fillData();
Button testingCsv =(Button)findViewById(R.id.btnTestCsv);
testingCsv.setOnClickListener(ChokeSlam);
testPopulate_Tests();
}
private OnClickListener ChokeSlam = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AssetManager aM = getAssets();
InputStream ipNext = null;
InputStreamReader iprNext = null;
try{
//ipNext = aM.open("Book1.csv");
//CSVReader reader = new CSVReader(new InputStreamReader(ipNext));
//iprNext = new InputStreamReader(ipNext);
//tp.ProcessInboundStream(ipNext);
tp.ProcessInboundStream(aM,"Book1.csv");
ipNext.close();
//tp.ProcessInboundStream(new InputStream(ipNext));
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
public class TestProcessor {
private DbManagement mdbManager;
private final Context mctx;
public TestProcessor(Context ctx) {
// TODO Auto-generated constructor stub
this.mctx = ctx;
}
public void ProcessInboundStream(AssetManager aM,String fileName){
InputStream ipNext = null;
try{
ipNext = aM.open(fileName);
CSVReader reader = new CSVReader(new InputStreamReader(ipNext));
ipNext.close();
//tp.ProcessInboundStream(new InputStream(ipNext));
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
}