1

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
Community
  • 1
  • 1
LadyWinter
  • 307
  • 6
  • 13

2 Answers2

0

Everything is fine..but to display line when you click next button u need to setonClickListener() for next Button.

    public class QuestionActivity extends ActionBarActivity {
    static InputStream inputStream;
    static InputStreamReader inputreader;
    static BufferedReader bufferedreader;
    String data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        TextView textView = (TextView)findViewById(R.id.hellotxt);
        Button next=(Button) findViewById(R.id.next); // next button id for suppose..

        inputStream = ctx.getResources().openRawResource(resId);

        inputreader = new InputStreamReader(inputStream);

        bufferedreader = new BufferedReader(inputreader);

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
             try 
             {
                 while (( line = bufferedreader.readLine()) != null) 
                 {
                  data+=line+"\n";
                 }
              } 
           catch (IOException e) {}

              textView.setText(data);
            }
          });

    } //onCreate

  } //main class
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

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?

In your current loop, you have each line being saved to the line variable, which is then appended to stringBuilder with a newline.

Instead of appending it to stringBuilder, you could add line to some sort of array each iteration, and then access the next element each time the user clicks the next button.

Or, you could do something with the returned stringBuilder and the newlines you add to the end of each line, such as split (which essentially gives you the same as the above, but using your already written code). So at the end of onCreate, you would call split to get an array of strings split by the newline character:

String[] lines = data.toString().split("\\n");

and then each time the user clicks the button, you could increment a counter and get the line for the element at counter, or you could get a little more complex and remove the element from the array as you read it (although you may not want an array in that case).

ipavl
  • 828
  • 11
  • 20
  • I had written this based on the code you originally posted, so it doesn't really apply the same now. – ipavl Oct 07 '14 at 03:19