0

所以,我正在尝试制作一个获取字幕(srt)文件并将其解析为 ListView 的应用程序,但从昨天开始,我收到了标题上的这个错误......这是我的 MainActivity.java

package com.doctororeo.examples;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ListActivity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MainActivity extends ListActivity {

protected static final String nl = "\\\n";
protected static final String sp = "[ \\t]*";
int size;
String fileName = "Dirty Harry VOST - Clint Eastwood.srt";

String pattern = "(?s)(\\d+)" + sp + nl
        + "([\\d]{2}:[\\d]{2}:[\\d]{2},[\\d]{3})" + sp + "-->" + sp
        + "([\\d]{2}:[\\d]{2}:[\\d]{2},[\\d]{3})" + sp + "(X1:\\d.*?)??"
        + nl + "(.*?)" + nl + nl;

private ArrayList<Item> m_parts = new ArrayList<Item>();
private Runnable viewParts;
private ItemAdapter m_adapter;
private String contents;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    InputStream input;
    AssetManager assetManager = getAssets();
    byte[] buffer = new byte[size];
    try {
        input = assetManager.open(fileName);
        size = input.available();
        input.read(buffer);
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String text = new String(buffer);
    Scanner in = new Scanner(text);

    try {
        String contents = in.nextLine();

        while (in.hasNextLine()) {
            contents = contents + "\n" + in.nextLine();
        }
    }

    finally {
        in.close();

    }

    // instantiate ItemAdapter class
    m_adapter = new ItemAdapter(this, R.layout.list_item, m_parts);
    setListAdapter(m_adapter);

    viewParts = new Runnable() {
        public void run() {
            handler.sendEmptyMessage(0);
        }
    };

    Thread thread = new Thread(null, viewParts, "MagentoBackground");
    thread.start();
}

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {

        Pattern r = Pattern.compile(pattern, Pattern.MULTILINE);

        // Now create matcher object.
        Matcher m = r.matcher(contents);
        while (m.find()) {
            m_parts.add(new Item(m.group(1), m.group(2), m.group(3), m
                    .group(5)));

            m_adapter = new ItemAdapter(MainActivity.this,
                    R.layout.list_item, m_parts);

            // display the list.
            setListAdapter(m_adapter);
        }
    };
};
}

项目适配器.java

package com.doctororeo.examples;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<Item> {

private ArrayList<Item> objects;

public ItemAdapter(Context context, int textViewResourceId,
        ArrayList<Item> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }

    Item i = objects.get(position);

    if (i != null) {

        TextView id = (TextView) v.findViewById(R.id.SubId);
        TextView start = (TextView) v.findViewById(R.id.SubStart);
        TextView end = (TextView) v.findViewById(R.id.SubEnd);
        TextView text = (TextView) v.findViewById(R.id.SubText);

        if (id != null) {
            id.setText(i.getId());
        }
        if (start != null) {
            start.setText(i.getStart());
        }
        if (end != null) {
            end.setText(i.getEnd());
        }
        if (text != null) {
            text.setText(i.getText());
        }

    }

    return v;

}

}

项目.Java

package com.doctororeo.examples;

public class Item {
private String id;
private String start;
private String end;
private String text;

public Item(){

}

public Item(String i, String d, String p, String f){
    this.id = d;
    this.start = i;
    this.end = p;
    this.text = f;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getStart() {
    return start;
}

public void setStart(String start) {
    this.start = start;
}

public String getEnd() {
    return end;
}

public void setEnd(String end) {
    this.end = end;
}
public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

}

我的日志

09-14 00:47:51.538: D/dalvikvm(1269): GC_FOR_ALLOC freed 56K, 8% free 2776K/2992K,         paused 46ms, total 49ms
09-14 00:47:51.558: I/dalvikvm-heap(1269): Grow heap (frag case) to 3.937MB for  1127536-byte allocation
09-14 00:47:51.629: D/dalvikvm(1269): GC_FOR_ALLOC freed 2K, 6% free 3875K/4096K, paused 71ms, total 71ms
09-14 00:47:51.689: D/AndroidRuntime(1269): Shutting down VM
09-14 00:47:51.689: W/dalvikvm(1269): threadid=1: thread exiting with uncaught exception (group=0x41465700)
09-14 00:47:51.709: E/AndroidRuntime(1269): FATAL EXCEPTION: main
09-14 00:47:51.709: E/AndroidRuntime(1269): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.doctororeo.examples/com.doctororeo.examples.MainActivity}:      java.util.NoSuchElementException
09-14 00:47:51.709: E/AndroidRuntime(1269):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.os.Looper.loop(Looper.java:137)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at  java.lang.reflect.Method.invoke(Method.java:525)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at dalvik.system.NativeStart.main(Native Method)
09-14 00:47:51.709: E/AndroidRuntime(1269): Caused by: java.util.NoSuchElementException
09-14 00:47:51.709: E/AndroidRuntime(1269):     at java.util.Scanner.nextLine(Scanner.java:1404)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at com.doctororeo.examples.MainActivity.onCreate(MainActivity.java:57)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.Activity.performCreate(Activity.java:5133)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-14 00:47:51.709: E/AndroidRuntime(1269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-14 00:47:51.709: E/AndroidRuntime(1269):     ... 11 more
09-14 00:48:04.237: I/Process(1269): Sending signal. PID: 1269 SIG: 9

和我的 main.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
>
<ListView
   android:id="@+id/android:list"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
/>
</LinearLayout>

我见过类似的错误答案,但它们根本没有帮助。

新日志

09-14 02:22:23.851: D/dalvikvm(1603): GC_FOR_ALLOC freed 46K, 7% free 2776K/2980K, paused 45ms, total 52ms
09-14 02:22:23.857: I/dalvikvm-heap(1603): Grow heap (frag case) to 3.937MB for 1127536-byte allocation
09-14 02:22:23.968: D/dalvikvm(1603): GC_FOR_ALLOC freed 2K, 6% free 3875K/4084K, paused 100ms, total 100ms
09-14 02:22:24.028: W/System.err(1603): java.util.NoSuchElementException
09-14 02:22:24.038: W/System.err(1603):     at   java.util.Scanner.nextLine(Scanner.java:1404)
09-14 02:22:24.038: W/System.err(1603):     at com.doctororeo.examples.MainActivity.onCreate(MainActivity.java:58)
09-14 02:22:24.038: W/System.err(1603):     at android.app.Activity.performCreate(Activity.java:5133)
09-14 02:22:24.038: W/System.err(1603):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-14 02:22:24.038: W/System.err(1603):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-14 02:22:24.038: W/System.err(1603):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-14 02:22:24.038: W/System.err(1603):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 02:22:24.048: W/System.err(1603):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-14 02:22:24.048: W/System.err(1603):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 02:22:24.048: W/System.err(1603):     at android.os.Looper.loop(Looper.java:137)
09-14 02:22:24.048: W/System.err(1603):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-14 02:22:24.058: W/System.err(1603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 02:22:24.058: W/System.err(1603):     at java.lang.reflect.Method.invoke(Method.java:525)
09-14 02:22:24.058: W/System.err(1603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-14 02:22:24.058: W/System.err(1603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-14 02:22:24.058: W/System.err(1603):     at dalvik.system.NativeStart.main(Native  Method)
09-14 02:22:24.138: W/System.err(1603): java.lang.NullPointerException
09-14 02:22:24.138: W/System.err(1603):     at java.util.regex.Matcher.reset(Matcher.java:177)
09-14 02:22:24.148: W/System.err(1603):     at java.util.regex.Matcher.<init>(Matcher.java:90)
09-14 02:22:24.148: W/System.err(1603):     at java.util.regex.Pattern.matcher(Pattern.java:297)
09-14 02:22:24.148: W/System.err(1603):     at com.doctororeo.examples.MainActivity$1.handleMessage(MainActivity.java:96)
09-14 02:22:24.148: W/System.err(1603):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 02:22:24.148: W/System.err(1603):     at android.os.Looper.loop(Looper.java:137)
09-14 02:22:24.158: W/System.err(1603):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-14 02:22:24.158: W/System.err(1603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 02:22:24.158: W/System.err(1603):     at   java.lang.reflect.Method.invoke(Method.java:525)
09-14 02:22:24.158: W/System.err(1603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-14 02:22:24.158: W/System.err(1603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-14 02:22:24.158: W/System.err(1603):     at dalvik.system.NativeStart.main(Native Method)
09-14 02:22:24.278: D/libEGL(1603): loaded /system/lib/egl/libEGL_emulation.so
09-14 02:22:24.358: D/(1603): HostConnection::get() New Host Connection established 0x2a050a60, tid 1603
09-14 02:22:24.458: D/libEGL(1603): loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-14 02:22:24.468: D/libEGL(1603): loaded /system/lib/egl/libGLESv2_emulation.so
09-14 02:22:24.608: W/EGL_emulation(1603): eglSurfaceAttrib not implemented
09-14 02:22:24.618: D/OpenGLRenderer(1603): Enabling debug mode 0

使用@nexus_2006 代码登录

09-14 04:01:43.837: D/dalvikvm(2040): GC_FOR_ALLOC freed 38K, 7% free 2776K/2972K, paused 44ms, total 50ms
09-14 04:01:43.847: I/dalvikvm-heap(2040): Grow heap (frag case) to 3.937MB for 1127536-byte allocation
09-14 04:01:43.977: D/dalvikvm(2040): GC_FOR_ALLOC freed 2K, 5% free 3875K/4076K, paused 127ms, total 127ms
09-14 04:01:44.307: D/dalvikvm(2040): GC_FOR_ALLOC freed 328K, 11% free 3987K/4476K, paused 37ms, total 38ms
09-14 04:01:44.447: W/System.err(2040): java.lang.NullPointerException
09-14 04:01:44.467: W/System.err(2040):     at java.util.regex.Matcher.reset(Matcher.java:177)
09-14 04:01:44.467: W/System.err(2040):     at java.util.regex.Matcher.<init>(Matcher.java:90)
09-14 04:01:44.487: W/System.err(2040):     at java.util.regex.Pattern.matcher(Pattern.java:297)
09-14 04:01:44.487: W/System.err(2040):     at com.example.subfile3.MainActivity$1.handleMessage(MainActivity.java:78)
09-14 04:01:44.487: W/System.err(2040):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 04:01:44.487: W/System.err(2040):     at android.os.Looper.loop(Looper.java:137)
09-14 04:01:44.497: W/System.err(2040):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-14 04:01:44.497: W/System.err(2040):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 04:01:44.497: W/System.err(2040):     at java.lang.reflect.Method.invoke(Method.java:525)
09-14 04:01:44.507: W/System.err(2040):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-14 04:01:44.507: W/System.err(2040):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-14 04:01:44.507: W/System.err(2040):     at dalvik.system.NativeStart.main(Native Method)
09-14 04:01:44.627: D/libEGL(2040): loaded /system/lib/egl/libEGL_emulation.so
09-14 04:01:44.627: D/(2040): HostConnection::get() New Host Connection established 0x2a050a30, tid 2040
09-14 04:01:44.721: D/libEGL(2040): loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-14 04:01:44.740: D/libEGL(2040): loaded /system/lib/egl/libGLESv2_emulation.so
09-14 04:01:44.861: W/EGL_emulation(2040): eglSurfaceAttrib not implemented
09-14 04:01:44.877: D/OpenGLRenderer(2040): Enabling debug mode 0
09-14 04:01:44.947: D/dalvikvm(2040): GC_FOR_ALLOC freed 586K, 16% free 3913K/4656K, paused 38ms, total 39ms
4

1 回答 1

0
try {
        String contents = in.nextLine();

        while (in.hasNextLine()) {
            contents = contents + "\n" + in.nextLine();
        }
    }

您的第一个问题是 while 循环,nextLine() 正在抛出 NoSuchElementException,大概是因为text它是空的(注意,nextLine() 会抛出这个特定的异常,hasNextLine() 不会抛出这个)。我将开始回顾使用一些Log.d语句检索该文件的过程或使用 Eclipse 中的调试内容。就个人而言,我会使用这样的 FileReader 并称之为好,而不是使用字符串、扫描仪等,但我很新,所以我知道什么?

    BufferedReader readIn;
    AssetManager assetManager = getAssets();
    String output;
    try {
        readIn = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        String contents;
        StringBuilder sB = new StringBuilder();
        while ((contents = readIn.readLine()) != null) {
            sB.append(contents);
        }
        output = sB.toString();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

编辑:我确实看到了一个实际问题,这可能与第 96 行的 Matcher 为空有关。 String contents =在 try 块内声明,在块外无法访问,将声明移到方法体中,这样你就有了

String contents;
try {
    while (in.hasNextLine()) {
        contents = ....
于 2013-09-14T07:03:17.247 回答