0

我正在构建一个 Android 应用程序来测试人们的主题,后端是用 Java 编写的,它读取和解析 CSV 文件(硬编码 URL),前端是我的 Android 应用程序。

这是我的错误:

05-08 01:19:00.792: E/AndroidRuntime(790): java.lang.RuntimeException: Unable to start activity ComponentInfo{cs314.adamnick.p4/cs314.adamnick.p4.QuizTakerGUI}: java.lang.NullPointerException
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.os.Looper.loop(Looper.java:123)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-08 01:19:00.792: E/AndroidRuntime(790):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 01:19:00.792: E/AndroidRuntime(790):  at java.lang.reflect.Method.invoke(Method.java:521)
05-08 01:19:00.792: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-08 01:19:00.792: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-08 01:19:00.792: E/AndroidRuntime(790):  at dalvik.system.NativeStart.main(Native Method)
05-08 01:19:00.792: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
05-08 01:19:00.792: E/AndroidRuntime(790):  at wharehouse.QuizAndroidGUI.<init>(QuizAndroidGUI.java:36)
05-08 01:19:00.792: E/AndroidRuntime(790):  at cs314.adamnick.p4.QuizTakerGUI.onCreate(QuizTakerGUI.java:38)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 01:19:00.792: E/AndroidRuntime(790):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-08 01:19:00.792: E/AndroidRuntime(790):  ... 11 more

这导致我的程序崩溃。

这是我的课:

public class QuizTakerGUI extends Activity {

    private RadioButton answer1;
    private RadioButton answer2;
    private RadioButton answer3;
    private RadioButton answer4;

    private Button submit; 
    private Button previous; 
    private Button next; 
    private Button finish; 

    private TextView questionArea; 

    private QuizAndroidGUI dataWharehouse;
    private QuizQuestion currentQuestion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_taker);

        dataWharehouse = new QuizAndroidGUI(this);

        answer1 = (RadioButton) findViewById(R.id.answer1);
        answer2 = (RadioButton) findViewById(R.id.answer2);
        answer3 = (RadioButton) findViewById(R.id.answer3);
        answer4 = (RadioButton) findViewById(R.id.answer4);

        questionArea = (TextView) findViewById(R.id.quizQuestion); 


        submit = (Button) findViewById(R.id.submitAnswer);
        previous = (Button) findViewById(R.id.prev);
        next = (Button) findViewById(R.id.next);
        finish = (Button) findViewById(R.id.finish);

        submit.setOnClickListener(submitHandler);
        previous.setOnClickListener(previousHandler);
        next.setOnClickListener(nextHandler);
        finish.setOnClickListener(finishHandler);
    }

这是我的数据仓库类:

public QuizAndroidGUI(QuizTakerGUI app){
    URL path = ClassLoader.getSystemResource("questions.csv");
    this.androidApplication = app; 
    NUMBERofQUIZES = 10; 
    // since we implement quiz ui we pass in the current program 
    quizTaker = new QuizTaker(this);
    try {
        // passing in the csv file hard coded 
        parser = new QuestionParser(new File(path.toURI()));
    } catch (FileNotFoundException e) {
        parser = null; 
        e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    quizTaker.startAndDisplayQuiz("Android Quiz", this.NUMBERofQUIZES, this.parser);
}

我认为这个问题可能是因为解析器被设置为空。

4

2 回答 2

1

questions.csv文件放入您的类路径并执行以下操作:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resource = loader.getResource("myfile");

正确加载文件。

于 2013-05-08T07:33:03.060 回答
0

没有足够的线路信息,但我怀疑这ClassLoader.getSystemResource("questions.csv")不是你真正想要的,至少在我的经验中,在 android 中看到它是非常不寻常的。

我会将文件放在assets目录中并使用AssetManager读取文件。

于 2013-05-08T07:42:00.610 回答