我有一个奇怪的问题。我有这个Android应用程序,应该从URL导致一个文本文件的一些数据。它有18行和每行一个项目,看起来像这样:
万事达
名称
123456789
1004
56
5000
(这是一项任务,所有数据显然是假的)
我有这个:
public class CreditCard {
//set up method to get and return data as arraylist
public ArrayList<String> ccArray(){
ArrayList<String> ccInfo = new ArrayList<String>();
//array set up. time to get data
try {
URL urlDat = new URL("url");
Scanner urlScn = new Scanner(urlDat.openStream());
while (urlScn.hasNext()){
ccArray().add(urlScn.nextLine() + "\n");
}
}catch (MalformedURLException URLe){
//handle exception
}
catch (IOException IOe){
//handle exception
}
return ccInfo;
}
}
所以我可以将数组传递给main方法(或者类中的另一个方法,还不确定)。我有一个按钮,在主方法中运行displayCreditCards:
public void displayCreditCards(View view) throws IOException {
TextView tv1;
tv1 = (TextView) findViewById(R.id.text_main);
tv1.setText("Data will appear here");
//create objects to det data and make profiles
CreditCard ccObj = new CreditCard();
tv1.setText(ccObj.ccArray().toString());
}
但是,这会导致应用程序无法响应,我从android获得了等待/关闭对话框。我不确定如何解释logcat:
10-04 18:27:01.204 9036-9036/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-04 18:27:01.204 9036-9036/? I/art﹕ Late-enabling JIT
10-04 18:27:01.206 9036-9036/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-04 18:27:01.257 9036-9036/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.pat.displaycc-2/lib/x86
10-04 18:27:01.396 9036-9058/com.example.pat.displaycc D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-04 18:27:01.400 9036-9036/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ad9a0, tid 9036
10-04 18:27:01.468 9036-9058/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ade40, tid 9058
10-04 18:27:01.471 9036-9058/com.example.pat.displaycc I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-04 18:27:01.520 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:01.521 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7917c0, error=EGL_SUCCESS
10-04 18:27:04.841 9036-9036/com.example.pat.displaycc W/art﹕ Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c20, error=EGL_SUCCESS
10-04 18:27:06.138 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f0d0
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c00, error=EGL_SUCCESS
10-04 18:27:13.108 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f060
我不知道为什么它会挂在我身上,任何想法?
答案 0 :(得分:0)
我认为问题是您在使用后不要关闭扫描仪。你应该总是在try-catch之后添加一个finally {}块,你应该在那里调用
urlScan.close() and
urlDat.close()