The problem may be familiar and may even be tagged as a duplicate. I have tried reading other problems similar to mine and tried to do it, might as say copied it (xD), but to no avail though.
I am new to Android Programming and I am trying to read a text file. I was able to read the whole text file, but what I want is to read the text file line by line. Then click the next button to display the next line. How I should do that?
This is what I have as of the moment. . .
public class QuestionActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
text = (TextView) findViewById(R.id.textView1);
try {
in = this.getAssets().open("hhquestion.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader = new BufferedReader(new InputStreamReader(in));
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText(line);
Button next = (Button) findViewById(R.id.btnNext);
next.setOnClickListener(this);
} //onCreate
public void onClick(View v){
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (line != null){
text.setText(line);
} else {
//you may want to close the file now since there's nothing more to be done here.
}
} //onClick
} //main class
I have tried the answer from this question, as this was the nearest solution to my question, but when I run the project, the app is closing. Thank you.
Logcat:
10-07 00:06:06.199: E/AndroidRuntime(1907): FATAL EXCEPTION: main
10-07 00:06:06.199: E/AndroidRuntime(1907): Process: asp.ons.scads, PID: 1907
10-07 00:06:06.199: E/AndroidRuntime(1907): java.lang.RuntimeException: Unable to start activity ComponentInfo{asp.ons.scads/asp.ons.scads.QuestionActivity}: java.lang.NullPointerException
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.os.Looper.loop(Looper.java:136)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-07 00:06:06.199: E/AndroidRuntime(1907): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 00:06:06.199: E/AndroidRuntime(1907): at java.lang.reflect.Method.invoke(Method.java:515)
10-07 00:06:06.199: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-07 00:06:06.199: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-07 00:06:06.199: E/AndroidRuntime(1907): at dalvik.system.NativeStart.main(Native Method)
10-07 00:06:06.199: E/AndroidRuntime(1907): Caused by: java.lang.NullPointerException
10-07 00:06:06.199: E/AndroidRuntime(1907): at asp.ons.scads.QuestionActivity.onCreate(QuestionActivity.java:43)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.Activity.performCreate(Activity.java:5231)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-07 00:06:06.199: E/AndroidRuntime(1907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-07 00:06:06.199: E/AndroidRuntime(1907): ... 11 more